0

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.

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.

I have included both the ASP and the C#, the sql should not be important as i cant even reach the call!

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

I know there is something missing here, the control is just never found no matter what i've tried!

Adam Garner
  • 1,235
  • 10
  • 17

1 Answers1

1

Instead of doing a FindControl, use an offset index of the column in question and get the first control:

(DropDownList)gvLocationArea.Rows[gvLocationArea.EditIndex].Cells[INDEX OF THE DDL].Controls[0]
Josh
  • 10,352
  • 12
  • 58
  • 109
  • Thanks for the response, but the control is still not found, it comes up null every single time... – Adam Garner Jun 04 '13 at 08:14
  • @AdamGarner Try .Controls[1]. If that doesn't work, is the DDL grid that creates the grid created during postback so the control exists in the control structure? – Josh Jun 04 '13 at 11:31
  • ah, does that mean that if the ddl is in the edit item template, it doesn't exist until AFTER it's data bound event? If so, is it even possible to find the control in the c#? – Adam Garner Jun 05 '13 at 08:06
  • 1
    @AdamGarner You can get access to it, you just need to rebind the datagrid, then check the edit item index, then call your code. What does the load code for the page look like? – Josh Jun 05 '13 at 12:18
  • Yes, that did it! i just needed to raise the databind even before looking for the control! cheers. – Adam Garner Jun 05 '13 at 13:46