简体   繁体   English

在ASP.NET中分页GridView(不会转到第2,3页,等等)

[英]Pagination of GridView in ASP.NET (Won't go to page 2,3, etc.)

I have a Gridview and I want to set Pagination . 我有一个Gridview ,我想设置Pagination So far I have: 到目前为止,我有:

<asp:GridView ID="grdActivity" 
              runat="server" 
              AutoGenerateColumns="False"  
              AllowPaging="True" 
              PageSize="30"     
              OnPageIndexChanging="gridView_PageIndexChanging">

C# code on the back is: 背面的C#代码为:

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostBack)
     {
          dsActivity myDataSet = new dsActivity();

          myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));

          grdActivity.DataSource = myDataSet.Tables["tblActivity"];

          grActivity.DataBind();
     }
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     grdActivity.PageIndex = e.NewPageIndex;
     grdActivity.DataBind();
}

I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. 我没有收到任何错误,但是当我按下任何其他页面(第2页及以上)时,什么也没有显示,只有空白页,并且GridView消失了。 Am I missing something? 我想念什么吗? I see hundreds of pages showing this exact code to do this. 我看到数百页显示了执行此操作的确切代码。

You will need to re-assign your datasource property. 您将需要重新分配数据源属性。 After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid. 回发后,grActivity.DataSource为null,并在其上调用DataBind将清除网格。

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     //re-assign your DataSource
     dsActivity myDataSet = new dsActivity();
     myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
     grdActivity.DataSource = myDataSet.Tables["tblActivity"];

     //Set the new page index
     grdActivity.PageIndex = e.NewPageIndex;

     //apply your changes
     grdActivity.DataBind();
}

Some notes: 一些注意事项:

  • you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see @ekad's answer) 您应该考虑将将数据带入到一个单独的方法中的代码重构,该方法称为Page_Load并位于gridview_PageIndexChanging内部(请参阅@ekad的答案)

  • if you have a lot of data you should consider paging the data directly on the database. 如果您有大量数据,则应考虑直接在数据库上分页数据。 Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid 现在,如果您的表有500行,即使其中的一小部分(页面大小)将显示在网格中,也会全部加载到数据集中

You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. 你要设置的数据源grdActivity调用之前grdActivity.DataBind()gridView_PageIndexChanging方法。 I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging 我还建议创建一个单独的方法,以避免在Page_LoadgridView_PageIndexChanging重复相同的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        grdActivity.DataSource = GetDataSource();
        grdActivity.DataBind();
    }
}

private DataTable GetDataSource()
{
    dsActivity myDataSet = new dsActivity();
    myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));

    return myDataSet.Tables["tblActivity"];
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdActivity.PageIndex = e.NewPageIndex;

    grdActivity.DataSource = GetDataSource();
    grdActivity.DataBind();
}

Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like 嗨,您需要将代码保存在单独的方法中,并在页面加载事件和页面索引更改事件中调用它,例如

protected void FillGrid()
{          
    dsActivity myDataSet = new dsActivity();

    myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));

    grdActivity.DataSource = myDataSet.Tables["tblActivity"];

    grActivity.DataBind();
}

and in the page index changing event 并在页面索引更改事件中

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   grdActivity.PageIndex = e.NewPageIndex;

   FillGrid();    
}

and in page load in !post back event simply call the method, like 然后在页面加载中!post back事件只需调用该方法,例如

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
     FillGrid();   
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM