在ASP.NET中的Gridview页脚模板中填充下拉列表时出错 [英] Error Populating Dropdownlist in Gridview Footer Template in ASP.NET

查看:103
本文介绍了在ASP.NET中的Gridview页脚模板中填充下拉列表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



I want to implement a cascading dropdownlist in a gridview footer template. My code is given below. I am getting an error message "Object reference not set to an instance of an object."

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 (ABCEntities dc = new ABCEntities())
        {
            all = dc.GrandParentService.ToList();
        }
        return all;
    }


private List<ParentService> GetParentServiceByGrandParent(int grandParentID)
    {

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

    }







请帮助我。在此先感谢。




Please help me. Thanks in advance.

推荐答案

使用用户定义的函数将数据绑定到GridView1_RowCreated事件中的gridview然后使用

bind data to gridview in GridView1_RowCreated event using user defined function then use
DropDownList ddlParent = (DropDownList)GridView1.Rows[e.NewIndex].FindControl("ddlParentService");



它会自动将dropdownlist ddlparentservice分配给ddlparent对象,然后将数据源分配给ddlparent对象。


It will automatically assign dropdownlist ddlparentservice to ddlparent object then assign datasource to ddlparent object.


这篇关于在ASP.NET中的Gridview页脚模板中填充下拉列表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆