在ASP.NET中的Gridview页脚模板中填充下拉列表时出错

时间:2013-12-30 18:39:00

标签: c# asp.net gridview

我想在gridview页脚模板中实现级联下拉列表。我的代码如下。我收到一条错误消息“对象引用未设置为对象的实例。

我的ASPX页面

 <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">

                <Columns>

                     <%--ServiceID--%>
                    <asp:TemplateField ItemStyle-Width="100px" HeaderText="ServiceID">
                        <ItemTemplate>
                            <asp:Label ID="lblServiceID" runat="server" Text='<%# Eval("ServiceId")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>



                    <%--Grand Parent Service --%>
                    <asp:TemplateField ItemStyle-Width="100px" HeaderText="Grand Parent">
                        <ItemTemplate>
                            <asp:Label ID="lblGrandParentService" runat="server" Text='<%# Eval("GrandService")%>'></asp:Label>

                            </ItemTemplate>

                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlGrandParentService" AutoPostBack="true" runat="server" Width="150" onselectedindexchanged="ddlGrandParentService_SelectedIndexChanged">
                            </asp:DropDownList>

                        </EditItemTemplate>
                        <FooterTemplate>
                              <asp:DropDownList ID="ddlGrandParentService" AutoPostBack="true" runat="server" Width="150" onselectedindexchanged="ddlGrandParentService_SelectedIndexChanged">  
                             </asp:DropDownList>  
                         </FooterTemplate>
                    </asp:TemplateField>


                    <%-- Parent Service --%>
                    <asp:TemplateField ItemStyle-Width="100px" HeaderText="Parent Service">
                        <ItemTemplate>
                            <asp:Label ID="lblParentService" runat="server" Text='<%# Eval("ParentServiceName")%>'></asp:Label>

                        </ItemTemplate>

                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlParentService" runat="server" Width="150" >
                            </asp:DropDownList>
                        </EditItemTemplate>

                        <FooterTemplate>
                            <asp:DropDownList ID="ddlParentService" runat="server" Width="150">
                            </asp:DropDownList>                            
                        </FooterTemplate>
                    </asp:TemplateField>
</Columns>                  
            </asp:GridView>



protected void ddlGrandParentService_SelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList ddlGrandParent = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddlGrandParent.NamingContainer;
        if (row != null)
        {
            if ((row.RowState & DataControlRowState.Edit) > 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<GrandParentService> GetGrandParentService()
{

    List<GrandParentService> all = new List<GrandParentService>();
    using (HospitalEntities dc = new HospitalEntities())
    {
        all = dc.GrandParentService.ToList();
    }

    return all;
}

private List<ParentService> GetParentServiceByGrandParent(int grandParentID)
{

    List<ParentService> all = new List<ParentService>();
    using (HospitalEntities dc = new HospitalEntities())
    {
        all = dc.ParentService.Where(a => a.GrandParentServiceID.Equals(grandParentID)).ToList();
    }

    return all;
}

请帮帮我。提前谢谢。

2 个答案:

答案 0 :(得分:2)

尝试将您设置ddlParent.DataSource的行更改为以下内容:

ddlParent.DataSource = GetParentServiceByGrandParent(Convert.ToInt32(ddlGrandParent.SelectedItem.Value));

您必须从当前DropDownList加载父EventArgs(就像您上面几行一样),而不是GridView.FooterRow。其次,你已经将父控件加载了几行。

答案 1 :(得分:1)

您无法在RowCreated事件中引用 GridView1.FooterRow ,因为它尚未创建。您应该使用 e.Row 替换参考。

ddlParent.DataSource = GetParentServiceByGrandParent  (Convert.ToInt32((GridView1.FooterRow.FindControl("ddlGrandParentService") as DropDownList).SelectedItem.Value));

应该是:

ddlParent.DataSource = GetParentServiceByGrandParent  (Convert.ToInt32((e.Row.FindControl("ddlGrandParentService") as DropDownList).SelectedItem.Value));