FormView.FindControl适用于同一页面上的一个表单但不适用于另一个表单

时间:2012-02-23 15:22:03

标签: asp.net formview findcontrol

我在同一页面上有两个Formview。我可以在没有问题的情况下首先使用FindControl,但它总是在第二个上失败。

我正在使用ItemTemplate,默认为ReadOnly,两者都绑定到SQLDataSources(不同的)但是我不能为我的生活解决为什么FindControl适用于一个而不是另一个。

我从下面的代码中删除了很多纯文本。

<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>

FindControl编码如下......

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

这些一直有用......

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

这总是返回null。在数据绑定完成之前,我已经阅读了一百万个关于控件未被渲染的内容,但是如果两个FOrmViews以相同的方式设置,结果应该是相同的。

任何帮助都会有很大帮助:)

马特:)

1 个答案:

答案 0 :(得分:0)

我想恳求愚蠢,但也提供一些背景知道我如何使用我从愚蠢中学到的东西。

我的DLFormView的SqlDataSource正在返回记录,当然我偶然使用了略有不同的值,因此没有返回任何记录。我只是通过使表单视图的ItemTemplate部分仅包含未绑定(基本上是纯文本)数据来解决这个问题。没有任何显示,这指向使用不同tempalte的页面。当我在EmptyDataTemplate中放置一个测试行时,它会显示,确认SqlDataSource没有返回任何内容。也许学校男孩在开发过程中没有在此模板中添加某些内容。

碰巧我需要使用EmptyDataTemplate,我还没有达到这一点。由于一次只渲染一个模板,因此我可以在两个模板中使用相同的ID名称。这意味着无需测试正在使用的模板。 (尽管DataItemsCount = 0时使用空模板,否则您可以测试其余模板的CurrentMode。)

我这是一个很大的失败,但我希望人们可以从我的错误中吸取教训。

马特:)

相关问题