在formview asp.net中,事件未按照预期进行控制

时间:2013-06-27 16:41:04

标签: asp.net formview

我在InsertItemTemplate中有一个LinkBut​​ton,单击它时,应该在InsertItemTemplate中显示一个隐藏的DropDownList。但是,它似乎不起作用,但它会说,当单击LinkBut​​ton时,更改Formview外的标签文本。事件正在触发,但是在InsertItemTemplate中使DropDownList可见的部分没有做任何事情。代码如下:

的.aspx:

<asp:FormView ID="formViewNewRecord" runat="server">
        <InsertItemTemplate>
            <asp:DropDownList ID="ddlAddSelection2" runat="server" DataSourceID="dSource1" DataTextField="Users"  DataValueField="Users" AppendDataBoundItems="true" Visible="false">
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <asp:LinkButton runat="server" ID="lbAddAnother" OnClick="lbAddAnother_Click">+Add Another</asp:LinkButton>
        </InsertItemTemplate>
        </asp:FormView>

    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

C#:

protected void lbAddAnother_Click(object sender, EventArgs e)
{
    DropDownList addSelection2 = (DropDownList)formViewNewItem.Row.Cells[0].FindControl("ddlAddSelection2");
    addSelection2.Visible = true;
    Label2.Text = addSelection2.ID;
}

1 个答案:

答案 0 :(得分:0)

您的下拉控件不是您的formview的直接子项。因此,由于FindControl调用不是递归的,因此您必须在窗体视图的子控件的正确位置搜索控件。 See this for the details但是从高层次来看,你需要的是:

DropDownList ctrl = (DropDownList)FormView1.Row.Cells[0].FindControl("ddlAddSelection2");

之后,您应该检查它是否为安全措施。

相关问题