You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET GridView分页实现:开启AllowPaging后页码不显示问题排查

ASP.NET GridView分页控件不显示问题解决

核心问题分析

你遇到的分页控件不显示、无法切换页面的问题,核心原因是未启用自定义分页模式。由于你通过VirtualItemCount手动指定总记录数,且自行从数据源获取对应页的数据,这种场景下必须开启AllowCustomPaging="true",否则GridView会沿用内置分页逻辑,导致分页控件不渲染或功能异常。

具体修复步骤

1. 修改GridView标记,开启自定义分页

在GridView控件中添加AllowCustomPaging="true",同时可显式配置分页模式确保导航可见:

<asp:GridView ID="recItemsGrid" runat="server" AutoGenerateColumns="false" Width="100%" BorderColor="#f5f5f5"
    BorderStyle="Solid" BorderWidth="1px" AllowPaging="true" PageSize="3" 
    OnPageIndexChanging="recItems_OnPageIndexChanging"
    AllowCustomPaging="true" PagerSettings-Mode="Numeric">
  • AllowCustomPaging="true":启用自定义分页,配合VirtualItemCount实现手动分页逻辑
  • PagerSettings-Mode="Numeric":指定分页控件显示数字页码(确保分页导航可见)

2. 优化后台冗余代码(可选)

你的GetItemsFromDataSource方法中重复设置了DataSource,可去掉冗余行:

protected void GetItemsFromDataSource(int pageIndex, int pageSize)
{
    var paging = new ReconciliationItemPagingRequest(pageIndex, pageSize);
    ReconciliationItemPagingResult recItemPagingResult =
        ReconciliationItemService.GetJournalItemDetails(Convert.ToInt32(ParentPage.AssignmentID), paging);

    recItemsGrid.VirtualItemCount = recItemPagingResult.TotalRecordCount;
    recItemsGrid.DataSource = recItemPagingResult.Data;

    if (recItemsGrid.VirtualItemCount == 0)
    {
        lblNoItems.Visible = true;
        recItemsGrid.Visible = false;
    }
    else
    {
        lblNoItems.Visible = false;
        recItemsGrid.Visible = true;
        recItemsGrid.Columns[0].Visible = recItemsGrid.VirtualItemCount > 1;
    }

    recItemsGrid.DataBind();            
} 

3. 验证总记录数正确性

确保recItemPagingResult.TotalRecordCount返回的是全部数据的总条数,而非当前页的记录数。如果该值等于当前页条数,GridView会判定只有一页,分页控件自然不会显示。

额外排查点

  • 检查CSS样式:确认页面中GridView的分页区域(如.gridview .pager类)未被设置display:none或visibility:hidden
  • 确认事件绑定:确保OnPageIndexChanging事件已正确关联到后台方法,页面回传时未重置GridView的核心属性

内容的提问来源于stack exchange,提问作者George110011

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:57:08