Last post Jul 14, 2017 08:36 AM by Kvetch
Jul 13, 2017 01:43 PM|Kvetch|LINK
I have a dropdownlist in an edit item template field. Everywhere I check says to use find control but
string pt = (gvProducts.Rows[e.RowIndex].FindControl("ddlMake") as DropDownList).SelectedItem.Value;
gives an object reference error when I try to update my gridview.
I've also tried DropDownList ddlModel = (DropDownList)gvProducts.Rows[e.RowIndex].FindControl("ddlModel");
My grid is only binded on !ispostback, and is also binded during rowediting.
Right now I'm setting the DDL selectedvalue to a label text in the rowdatabound/DDL index changed events, making it public and then converting them in my row update event.
But that's not very efficient and I'll have about fifteen labels lying around in the full gridview.
Jul 13, 2017 08:49 PM|jimmy69|LINK
perhpas read these article about dropdownlist inside a gridview
Jul 13, 2017 11:34 PM|A2H|LINK
You can try with the below code.
<asp:GridView ID="GridView2" runat="server" DataKeyNames="Name" AutoGenerateColumns="false" OnRowUpdating="GridView2_RowUpdating" OnRowEditing="GridView2_RowEditing">
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
<asp:DropDownList ID="ddlMake" runat="server">
<asp:ListItem Text="TextValue1" Value="One"></asp:ListItem>
<asp:ListItem Text="TextValue2" Value="Two"></asp:ListItem>
<asp:ListItem Text="TextValue3" Value="Three"> So</asp:ListItem>
<asp:CommandField ShowEditButton="true" />
protected void Page_Load(object sender, EventArgs e)
public void BindData()
DataTable dt = new DataTable();
GridView2.DataSource = dt;
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
GridView2.EditIndex = e.NewEditIndex;
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
//Finding Dropdownlist control
string drpdownseletedvalue = (GridView2.Rows[e.RowIndex].FindControl("ddlMake") as DropDownList).SelectedItem.Value;
Jul 13, 2017 11:36 PM|A2H|LINK
Also by any chance are you hiding the dropdownlist control using Visible property by setting to false. if so then you wont be able access the control in code behind as the control wont render on page if hiding the control using Visible property
Jul 14, 2017 08:36 AM|Kvetch|LINK
Figured out what was wrong by comparing my code to A2H's. My dropdownlist ID was actually something different so it was looking for something that didn't exist.