对象引用未设置为Gridview中对象的实例

时间:2011-11-01 09:14:19

标签: asp.net

我在该网格中有一个网格名称= gvDetails我还有一个用于页面导航的下拉列表

protected void gvDetails_DataBound(object sender, EventArgs e)
{
    GridViewRow pagerRow = gvDetails.BottomPagerRow;
   DropDownList pageList = 
  

(DropDownList的)pagerRow.Cells [0] .FindControl( “PageDropDownList”);

    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
    if (pageList != null)
    {
        for (int i = 0; i < gvDetails.PageCount; i++)
        {
            int pageNumber = i + 1;
            ListItem item = new ListItem(pageNumber.ToString());
            if (i == gvDetails.PageIndex)
            {
                item.Selected = true;
            }
            pageList.Items.Add(item);
        }
    }
    if (pageLabel != null)
    {
        int currentPage = gvDetails.PageIndex + 1;
    }
}

- - - - * ** * ** ----------

这是我的填充编码

protected void gvDetails_DataBound(object sender, EventArgs e)
{
    GridViewRow pagerRow = gvDetails.BottomPagerRow;
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
    if (pageList != null)
    {
        for (int i = 0; i < gvDetails.PageCount; i++)
        {
            int pageNumber = i + 1;
            ListItem item = new ListItem(pageNumber.ToString());
            if (i == gvDetails.PageIndex)
            {
                item.Selected = true;
            }
            pageList.Items.Add(item);
        }
    }
    if (pageLabel != null)
    {
        int currentPage = gvDetails.PageIndex + 1;
    }
}
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow pagerRow = gvDetails.BottomPagerRow;
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
    gvDetails.PageIndex = pageList.SelectedIndex;
}
private void LoadBindGrid()
{
    ////Set your connection String here
    SqlConnection CN = DBUtil.GetCon();
    if (CN.State != ConnectionState.Closed)
        CN.Close();
    CN.Open();
    string q = "SELECT Head_Code,Head_Name,Section_Code,Head_Mode,Head_Type,Head_Status FROM eLogs_Acc_Account_Master";
    SqlCommand cmd = new SqlCommand(q, CN);
    //SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
    ////Write a select Query
    //string q = "select CustomerID,CompanyName,ContactName,Phone,City,Country  from Customers";
    DataSet dset = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(q, CN);
    //cn.Open();
    DataTable dtable = new DataTable();
    da.Fill(dtable);
    //Bind grid.
    gvDetails.DataSource = dtable;
    gvDetails.DataBind();
    CN.Close();
}

对象引用未设置为对象的实例。 IE。,从第4行(DropDownList pageList =(DropDownList)pagerRow.Cells [0] .FindControl(“PageDropDownList”);) 请帮忙

1 个答案:

答案 0 :(得分:0)

请检查空值并提前退出。这就是我的意思:

protected void gvDetails_DataBound(object sender, EventArgs e)
{
    GridViewRow pagerRow = gvDetails.BottomPagerRow;
    //-- quick exit if pagerRow not found
    if(pagerRow == null) return;

DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
if(pageList==null) return;

Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");


    for (int i = 0; i < gvDetails.PageCount; i++)
    {
        int pageNumber = i + 1;
        ListItem item = new ListItem(pageNumber.ToString());
        if (i == gvDetails.PageIndex)
        {
            item.Selected = true;
        }
        pageList.Items.Add(item);
    }

if (pageLabel != null)
{
    int currentPage = gvDetails.PageIndex + 1;
}

}

请注意,上面的代码只是更正,以确保您永远不会得到对象引用错误,您必须确保它自己正常工作。

快乐编码