简体   繁体   English

在asp.net C#中的GridView编辑项目模板中从存储过程中找到下拉列表控件

[英]Finding drop down list control from a stored procedure in edit item template of gridview in asp.net c#

I am trying to find a DropDownList control on the EditItemTemplate of a grid view, to populate it with results from a query before it is drawn, but the control is never found. 我试图在网格视图的EditItemTemplate上找到一个DropDownList控件,以便在绘制之前用查询结果填充它,但从未找到该控件。

ddlParent == null

Always! 总是!

I may be missing something very obvious but i have tried about 8 different methods to get this find control working, but no matter what i do, it comes up null. 我可能会遗漏一些非常明显的东西,但是我尝试了8种不同的方法来使此查找控件正常工作,但是无论我做什么,它都会变为null。

I have included both the ASP and the C#, the sql should not be important as i cant even reach the call! 我已经包括了ASP和C#,SQL应该不重要,因为我什至无法拨打电话!

ASP: 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#: 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", ""));
                }
            }
        }
    }

I know there is something missing here, the control is just never found no matter what i've tried! 我知道这里缺少一些东西,无论我尝试过什么,都永远找不到控件!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM