开发者

How to get reference of DropDownList into codebehind?

<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&开发者_C百科gt;
                </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>

How to get reference of DropDownList2 in DropDownList1_SelectedIndexChanged when grid is in edit mode?


After editing your question. you can achieve like...

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


You should be able to access it in GridView1_RowEditing. You'll need to assign it to a control using something like:

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

EDIT: Based on the new info, this should do it:

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


I don't think you can get a reference to the dropdown in the codebehind since it doesn't exist. The dropdown will only exist in update mode in your example so you're out of luck there. However, you might be able to add one dynamically, that is, creating GridView or adding the templates in the codebehind.

If you provide some more information about what your actual problem is we might find an alternative solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜