【问题标题】:Error Populating Dropdownlist in Gridview Footer Template in ASP.NET在 ASP.NET 的 Gridview 页脚模板中填充下拉列表时出错
【发布时间】:2013-12-31 04:39:59
【问题描述】:

我想在 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;
}

请帮助我。提前致谢。

【问题讨论】:

  • 您的页脚模板中是否有 ID 为“ddlGrandParentService”和“ddlGrandParentService”的下拉菜单?错误说它找不到 ID 为“ddlParentService”的控件。
  • 是的,我有两个下拉列表。
  • 你究竟是从哪里得到 NullReferenceException 的?就行了"DropDownList ddlParent = (DropDownList)e.Row.FindControl("ddlParentService");"还是就行了“ddlParent.DataSource = GetParentServiceByGrandParent(Convert.ToInt32((GridView1.FooterRow.FindControl("ddlGrandParentService") as DropDownList).SelectedItem.Value));”?

标签: c# asp.net gridview


【解决方案1】:

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

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

您必须从当前 EventArgs 加载父 DropDownList(就像您在上面的几行中所做的那样),而不是从 GridView.FooterRow。其次,您已经在上面几行加载了父控件。

【讨论】:

  • 非常感谢凯文。但是父下拉列表不会过滤祖父下拉列表。正如我在上面的代码中注释掉的那样,我如何获得祖父下拉列表的选定值。
  • 对不起,我实际上不明白你的要求。您能否更新上面的代码并添加 控件的定义。
【解决方案2】:

您不能在 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));

【讨论】:

  • 这不是一个答案...你最好使用 cmets。
  • @perror - 为什么不是答案?不争论,只是努力学习。
  • 您似乎在询问有关原始问题的更多信息。如果您确定自己的答案(可能是这种情况),只需说您认为该行应该被第二行替换(并删除最后的“?”)。这真的是在这里展示东西的问题。
  • 感谢您的帮助。我已经编辑了答案以使其更清晰。
  • 是的,这样会好很多。祝下次好运! ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多