0

I am trying to manipulate a DropDownList, which is in a field of a GridView, from code-behind in ASP.NET. I am doing this by adding the reference to a local DDL with the FindControl method. However, it doesn't seem to work, I have tried multiple approaches (Load, Init Events) but I always get a NullReferenceException.

Here is my code:

 <asp:TemplateField HeaderText="Zuständige Führungskraft">
           <ItemTemplate>
           <!-- <%# Eval("ZustaendigeFuehrungskraft")%> -->
                <asp:DropDownList AppendDataBoundItems="true" Enabled="false" runat="server" ID="ddwnFK" >

                </asp:DropDownList>
           </ItemTemplate>
           <EditItemTemplate>
                <asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnFK" >

                </asp:DropDownList>
           </EditItemTemplate>
</asp:TemplateField> 


 protected void grdBenutzer_RowEditing(object sender, GridViewEditEventArgs e)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT Nachname, Vorname FROM Benutzer WHERE Benutzerart='Führungskraft' AND Archiviert != 1", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable table = new DataTable();
            table.Load(dr);
            foreach (GridViewRow row in grdBenutzer.Rows)
            {
                DropDownList ddwnFK = (DropDownList)row.FindControl("ddwnFK");
                //if (ddwnFK == null)
                //    continue;

                ddwnFK.Items.Add("keine");

                foreach (DataRow dtRow in table.Rows)
                {
                    ddwnFK.Items.Add(dtRow["Nachname"].ToString() + ", " + dtRow["Vorname"].ToString());

                }
            }
        }
LeonidasFett
  • 3,052
  • 4
  • 46
  • 76
  • Comment about FindControl from MSDN: This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls. Try to explore the tree of controls under row. May be, drop down is not a direct child. – Vladimir Mar 26 '13 at 10:22
  • thanks...what is the container in this case? gridview or single cell? – LeonidasFett Mar 26 '13 at 10:58
  • I think that most plausibly a single cell is a container. – Vladimir Mar 26 '13 at 11:54

2 Answers2

0

can you try access it as

DropDownList ddwnFK = (DropDownList)row.Cells[0].Controls[0];

or see in the Quick watch what are the contents of row there.

Rajneesh
  • 2,185
  • 4
  • 20
  • 30
0

It seems you're trying to find your drop down in every row of your grid (even header/footer row doesn't contain your drop down).

You can access the row being edited by using GridViewEditEventArgs.NewEditIndex property:

var row = grdBenutzer.Rows[e.NewEditIndex];
var ddwnFK = (DropDownList)row.FindControl("ddwnFK");
Oleks
  • 31,955
  • 11
  • 77
  • 132