访问C#中GridView中的DropDownList



我在ASP.net 的Webform中的GridView中有以下代码

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

我想在C#中的代码隐藏文件中访问DropDownList。我尝试了很多方法使用查找控制方法,但没有成功。请引导我。

DropDownList存在于GridView中的所有行中。如果您希望这样做,您需要找到特定的行。

如果你知道行索引,他们会试试这个:

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

或者,您也可以在GridView的Row Databound事件中查找所有行。

DropDownList myDDL = (DropDownList)e.Row.FindControl("ddlDesignation");

在Gridview RowDataBound中尝试以下操作:

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

在GridView的RowDataBound中:

  protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (gdv.HeaderRow != null)
            {
                TextBox txt = (TextBox)gdv.HeaderRow.FindControl("txt");
            }
        }

最新更新