Note that there are some explanatory texts on larger screens.

plurals
  1. POError Populating Dropdownlist in Gridview Footer Template in ASP.NET
    text
    copied!<p>I want to implement a cascading dropdownlist in a gridview footer template. My code is given below. I am getting an error message "<strong>Object reference not set to an instance of an object.</strong>"</p> <p>My ASPX Page</p> <pre><code> &lt;asp:GridView ID="GridView1" runat="server" Width="950px" AutoGenerateColumns="false" Font-Names="Segoe UI Symbol" Font-Size="11pt" AlternatingRowStyle-BackColor="White" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" AllowPaging="true" ShowFooter="true" RowStyle-BackColor="#A1DCF2" OnRowEditing="GridView1_RowEditing" DataKeyNames="ServiceID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCreated ="GridView1_RowCreated" PageSize="20" CellPadding="4"&gt; &lt;Columns&gt; &lt;%--ServiceID--%&gt; &lt;asp:TemplateField ItemStyle-Width="100px" HeaderText="ServiceID"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblServiceID" runat="server" Text='&lt;%# Eval("ServiceId")%&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;%--Grand Parent Service --%&gt; &lt;asp:TemplateField ItemStyle-Width="100px" HeaderText="Grand Parent"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblGrandParentService" runat="server" Text='&lt;%# Eval("GrandService")%&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="ddlGrandParentService" AutoPostBack="true" runat="server" Width="150" onselectedindexchanged="ddlGrandParentService_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:DropDownList ID="ddlGrandParentService" AutoPostBack="true" runat="server" Width="150" onselectedindexchanged="ddlGrandParentService_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;%-- Parent Service --%&gt; &lt;asp:TemplateField ItemStyle-Width="100px" HeaderText="Parent Service"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblParentService" runat="server" Text='&lt;%# Eval("ParentServiceName")%&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="ddlParentService" runat="server" Width="150" &gt; &lt;/asp:DropDownList&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:DropDownList ID="ddlParentService" runat="server" Width="150"&gt; &lt;/asp:DropDownList&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; protected void ddlGrandParentService_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddlGrandParent = (DropDownList)sender; GridViewRow row = (GridViewRow)ddlGrandParent.NamingContainer; if (row != null) { if ((row.RowState &amp; DataControlRowState.Edit) &gt; 0) { DropDownList ddlParent = (DropDownList)row.FindControl("ddlParentService"); ddlParent.DataSource = GetParentServiceByGrandParent(Convert.ToInt32(ddlGrandParent.SelectedValue)); ddlParent.DataValueField = "ParentServiceID"; ddlParent.DataTextField = "ParentServiceName"; ddlParent.DataBind(); } } } public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Footer) { DropDownList ddlGrandParent = (DropDownList)e.Row.FindControl("ddlGrandParentService"); ddlGrandParent.DataSource = GetGrandParentService(); ddlGrandParent.DataValueField = "GrandParentServiceID"; ddlGrandParent.DataTextField = "GrandService"; ddlGrandParent.DataBind(); //ddlGrandParent.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); DropDownList ddlParent = (DropDownList)e.Row.FindControl("ddlParentService"); //**Getting error "Object reference not set to an instance of an object."** ddlParent.DataSource = GetParentServiceByGrandParent (Convert.ToInt32((GridView1.FooterRow.FindControl("ddlGrandParentService") as DropDownList).SelectedItem.Value)); ddlParent.DataValueField = "ParentServiceId"; ddlParent.DataTextField = "ParentServiceName"; ddlParent.DataBind(); } } private List&lt;GrandParentService&gt; GetGrandParentService() { List&lt;GrandParentService&gt; all = new List&lt;GrandParentService&gt;(); using (HospitalEntities dc = new HospitalEntities()) { all = dc.GrandParentService.ToList(); } return all; } private List&lt;ParentService&gt; GetParentServiceByGrandParent(int grandParentID) { List&lt;ParentService&gt; all = new List&lt;ParentService&gt;(); using (HospitalEntities dc = new HospitalEntities()) { all = dc.ParentService.Where(a =&gt; a.GrandParentServiceID.Equals(grandParentID)).ToList(); } return all; } </code></pre> <p>Please help me. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload