【问题标题】:Pagination on asp:DataGrid does not workasp上的分页:DataGrid不起作用
【发布时间】:2016-01-16 12:16:57
【问题描述】:

我正在尝试将分页添加到我的 Gridview。我在属性中添加了以下内容:

<table id="myTable" class="table tbody" runat="server" visible="false">
     <tbody>
         <tr>
            <td>
               <asp:DataGrid ID="myGrid" runat="server" CssClass="table table-striped tbody" Visible="false"
                      AutoGenerateColumns="True"
                      AllowPaging="True"
                      AllowCustomPaging="true"
                      ForeColor="black"
                      HeaderStyle-Font-Bold="true"
                      HeaderStyle-ForeColor="black"
                      GridLines="None"
                      EnableViewState="false" />
             </td>
          </tr>
      </tbody>
</table>

结果集返回 22 行,它在第一页上正确显示了前 10 行,但我没有任何选项可以移动到下一页。没有数字或箭头可以移动到第 2 页等。

谁能帮我解决我做错了什么?

【问题讨论】:

    标签: c# asp.net datagrid pagination


    【解决方案1】:

    我认为您应该删除 Allow Custom paging property 并根据您的要求设置 PageSize 属性。然后您必须将OnPageIndexChanging 事件添加到您的网格中。比如:

    AllowPaging="true" PageSize="8" OnPageIndexChanging="stockTakeGrid_PageIndexChanging"
    

    所以在后面的代码中你还必须处理上面的事件例如:

            protected void stockTakeGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            stockTakeGrid.DataSource = ViewState["stockDetails"];
            stockTakeGrid.PageIndex = e.NewPageIndex;
            stockTakeGrid.AutoGenerateColumns = false;
            stockTakeGrid.DataBind();
        }
    

    最初加载网格时,将页面索引设置为 0

    【讨论】:

    • 我的视图状态是什么?
    • 任何你想命名的东西。然后只需调用相同的视图状态名称即可从中提取数据。我使用 ViewState 来最大程度地减少在页面生命周期中需要进行的数据库调用次数。
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2012-05-29
    • 2015-12-31
    相关资源
    最近更新 更多