如何将DropDownList引用到后面的代码中?

时间:2011-05-11 08:18:54

标签: c# .net asp.net gridview updatepanel

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing">
    <Columns>
        <asp:TemplateField HeaderText="Test">
            <EditItemTemplate>
                <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                            <asp:ListItem>Test1</asp:ListItem>
                            <asp:ListItem>Test2</asp:ListItem>
                            <asp:ListItem>Test3</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("SS") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rate">
            <EditItemTemplate>
                <asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

当网格处于编辑模式时,如何在DropDownList2中引用DropDownList1_SelectedIndexChanged

3 个答案:

答案 0 :(得分:1)

编辑完问题后。你可以实现...... ...

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList DropDownList2 = ((DropDownList)((DropDownList)sender).Parent.FindControl("DropDownList2"));
}

答案 1 :(得分:1)

您应该可以在GridView1_RowEditing中访问它。您需要使用以下内容将其分配给控件:

DropDownList list = e.Row.FindControl("DropDownList1") as DropDownList;

编辑:根据新信息,我们应该这样做:

int index = -1;

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 index = e.Row.Index;
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
 DropDownList DropDownList2 = GridView1.Rows[index].FindControl("DropDownList2") as DropDownList;
}

答案 2 :(得分:0)

我认为你不能引用代码隐藏中的下拉列表,因为它不存在。下拉列表仅在您的示例中以更新模式存在,因此您在那里运气不佳。但是,您可以动态添加一个,即创建GridView或在代码隐藏中添加模板。

如果您提供有关实际问题的更多信息,我们可能会找到替代解决方案。

相关问题