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());
}
}
}