在asp.net c#的gridview编辑项模板中查找存储过程中的下拉列表控件

本文关键字:存储 查找 存储过程 下拉列表 过程中 控件 net asp gridview 编辑 | 更新日期: 2023-09-27 18:16:47

我试图在网格视图的EditItemTemplate上找到一个DropDownList控件,在绘制之前用查询结果填充它,但从未找到该控件。

ddlParent == null

永远!

我可能错过了一些非常明显的东西,但我已经尝试了大约8种不同的方法来获得此查找控制工作,但无论我做什么,它出现null。

我已经包括了ASP和c#, sql不应该是重要的,因为我甚至不能到达调用!

ASP:

            <asp:TemplateField SortExpression="LocationArea2.Name" HeaderText="Parent Location Area">
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Eval("LocationArea2.Name") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlParent" runat="server" AppendDataBoundItems="true" DataTextField="LocationArea2.Name"
                        DataValueField="ParentID" AutoPostBack="false" SelectedValue='<%# Bind("ParentID") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
c#:

protected void gvLocationArea_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (gvLocationArea.EditIndex == e.Row.RowIndex)
        {
            DropDownList ddlParent = (DropDownList)gvLocationArea.Rows[gvLocationArea.EditIndex].FindControl("ddlParent");
            if (ddlParent != null)
            {
                using (SalesSQLEntities db = new SalesSQLEntities())
                {
                    ddlParent.DataSource = db.GetRecursiveAreaList(Convert.ToInt32(((TextBox)gvLocationArea.Rows[gvLocationArea.EditIndex].FindControl("txtLocationAreaID")).Text), true);
                    ddlParent.DataBind();
                    ddlParent.Items.Add(new ListItem("* None", ""));
                }
            }
        }
    }

我知道这里缺少了什么,不管我怎么努力,控制就是找不到!

在asp.net c#的gridview编辑项模板中查找存储过程中的下拉列表控件

不使用FindControl,而是使用相关列的偏移索引并获得第一个控件:

(DropDownList)gvLocationArea.Rows[gvLocationArea.EditIndex].Cells[INDEX OF THE DDL].Controls[0]