当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - Accessing DropDownList inside GridView in C#

问题描述:

I have following code in my GridView in Webform in ASP.net

<columns>

<asp:TemplateField SortExpression="DESIGNATION">

<HeaderTemplate>

<asp:Literal ID="Literal1" runat="server">Designation<br /></asp:Literal>

<asp:DropDownList ID="ddlDesignation" runat="server" BorderColor="#0000CC"></asp:DropDownList>

</HeaderTemplate>

<ItemTemplate>

<asp:Literal ID="Literal2" runat="server" Text='<%# Bind("DESIGNATION") %>'></asp:Literal>

</ItemTemplate>

</asp:TemplateField>

</columns>

<asp:TemplateField SortExpression="CNIC">

<HeaderTemplate>

<asp:Literal ID="Literal1" runat="server">CNIC<br /></asp:Literal>

<asp:TextBox runat="server" ID="searchBox" BorderColor="#0000CC"></asp:TextBox>

</HeaderTemplate>

<ItemTemplate>

<asp:Literal ID="Literal2" runat="server" Text='<%# Bind("CNIC") %>'></asp:Literal>

</ItemTemplate>

</asp:TemplateField>

I want to access DropDownList in my code behind file in C#.

I have Tried in many ways using find control method but did not work.Please guide me.

网友答案:

In your Gridview RowDataBound Try some thing like this:

protected void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList drp = (DropDownList)e.Row.FindControl("ddlDesignation");
        }
}
网友答案:

The DropDownList exists inside all rows in the GridView. You need to find the specific row if you wish to do that.

If you know the row index, they try this:

DropDownList myDDL = (DropDownList)gview.Row(index).FindControl("ddlDesignation");

Or you can also find all rows in the Row Databound event of the GridView.

DropDownList myDDL = (DropDownList)e.Row.FindControl("ddlDesignation");
网友答案:

In your RowDataBound of GridView:

  protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (gdv.HeaderRow != null)
            {
                TextBox txt = (TextBox)gdv.HeaderRow.FindControl("txt");
            }
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: