【问题标题】:Paging not working in Datagrid?分页在 Datagrid 中不起作用?
【发布时间】:2014-12-17 07:12:33
【问题描述】:

我正在从数据库中搜索一些数据到数据网格中。当我对其应用分页时,单击分页的下一个链接时,数据网格消失而没有显示任何内容。我在页面加载时也使用了 datagrid.databind(),当它是回发时,或者我什至创建了一个名为 datagridname_onpageindexchanged() 的回发事件方法:

protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
   DataGridSearchResults.currentpageindex=e.newpageindex;
   DataGridSearchResults.databind();
}

【问题讨论】:

    标签: c# asp.net datagrid


    【解决方案1】:

    您的 aspx 应包含分页:-

    <asp:DataGrid ID="DataGridSearchResults" runat="server" AllowPaging="true" PageSize="10"
    OnPageIndexChanged="DataGridSearchResults_PageIndexChanged">
    

    如果网格在页面加载时有数据,它应该像这样绑定:-

    protected void Page_Load(object sender, EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                    //Bind your grid here.
                }
        }
    

    然后页面索引改变功能:-

    protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
              DataGridSearchResults.CurrentPageIndex = e.NewPageIndex;
              //Bind your Grid here again.
    
    }
    

    【讨论】:

    • 只是为了更清楚地说明 AllowPaging 与 AllowCustomPaging 之间的区别。我的一些页面两者都有。但是,在我的模式中,当我使用 AllowCustomPaging 时,它拒绝添加分页,但是当我将其更改为 AllowPaging 时,它可以工作??????
    【解决方案2】:

    如果(!IsPostBack) {

     // Bind your dataGrid
    

    }

    查看此链接http://msdn.microsoft.com/enus/library/system.web.ui.webcontrols.datagrid.allowcustompaging.aspx

    【讨论】:

      【解决方案3】:

      您需要在此处为​​您的数据网格设置 DataSource。

      protected void DataGridSearchResults_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
      {
         DataGridSearchResults.CurrentPageIndex = e.NewPageIndex;
         DataGridSearchResults.DataSource = YourDataSource;
         DataGridSearchResults.DataBind();
      }
      

      另外,如果您在 Page_Load 中绑定您的网格,请确保您没有在 if(!IsPostBack){} 之外绑定您的网格。否则,您将在每次回发时丢失数据。

      protected void Page_Load(object sender, EventArgs e)
      {
          if(!IsPostBack)
          {
             //Bind Your Grid Here
          }
      }
      

      【讨论】:

      • 我按照你说的做了,它显示错误为“Invalid CurrentPageIndex value. It must be >= 0 and
      • @Sarthak 我已经用 DataGridSearchResults.PageIndex = e.NewPageIndex; 更新了我的答案;
      • 好吧,“.PageIndex”并不是网格的实际属性。所以视觉工作室的智能感知没有检测到它。
      • @Sarthak 用 PageIndex 替换为 CurrentPageIndex
      • 仍然是同样的错误,而且在页面加载时没有绑定数据,而是在单击另一个按钮时。
      猜你喜欢
      • 2018-10-27
      • 2016-01-16
      • 2012-05-29
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2018-03-05
      • 2012-04-18
      • 2015-05-31
      相关资源
      最近更新 更多