0

I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.

I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.

I have removed a lot of the plain text from the code below.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>" 
            SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = @AppID)" >
        <SelectParameters>
            <asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID" 
        DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <h1>
                <asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' /> 
                <asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
                <asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
            </h1>
            <p>Text here</p>
            <p><asp:TextBox ID="LicenseTextBox" runat="server"
            Text='<%# Eval("License")%>'
            TextMode="MultiLine"  Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>

            <asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
        </ItemTemplate>
    </asp:FormView>

    <p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>

    <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
        ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>" 
        SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = @AppID) AND ([Username] = @Username))" >
        <SelectParameters>
            <asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
            <asp:FormParameter FormField="Username" Name="Username" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

    <asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4" 
        DataKeyNames="AppID,Username">
        <ItemTemplate>
            <p> <asp:Label ID="DLAppNameLabel" runat="server" /> 
                <br />
                <br />
                <asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
                <br />
                <asp:HiddenField ID="TotalInstallations" runat="server" />
                Number of New Installations: 
                <asp:DropDownList ID="NumberOfNewInstallations" runat="server">
                </asp:DropDownList>
                <br />
                <asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
            </p>
        </ItemTemplate>
    </asp:FormView>

And the FindControl coding is as follows...

TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");

These always work...

Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");

This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.

Any help would be much apreciated :)

Matt :)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Matty W
  • 65
  • 1
  • 2
  • 8
  • I'd just like to add that I've also checked whether the SqlDataSources are returning records and they both are. – Matty W Feb 23 '12 at 15:33
  • I've noticed that when I step through the code and reach `TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");` The debugger jumps into the aspx page and looks through all the asp fields in FormView1. When I reach `Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");` The code does not jump to the aspx page adn therefore does not search for any asp controls. Anyone know why? – Matty W Feb 27 '12 at 12:06
  • I have tried a few more things now to no avail. I have tried putting DLFormView first in the aspx page and trying to find the label in DLFormView before FormView1; I have tried to use FindControl on other controls on the page but it won't find any at all, even at the page level (This idea here was to find the parent of the label and then use that object to find the label). FindControl only works for FormView1 and I can't work out why. Some help would be much appreciated. :) – Matty W Feb 28 '12 at 09:44

1 Answers1

0

I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.

My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.

As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)

So a big fail on my part, but I hope people can learn from my mistake.

Matt :)

Matty W
  • 65
  • 1
  • 2
  • 8