AspNetPager

本文详细介绍了如何在ASP.NET应用中使用AspNetPager与GridView实现分页功能,包括配置步骤、解决常见问题及代码实现。通过合理利用这两个控件,开发者能够有效地管理和展示大量数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

使用AspNetPager与GridView完成分页

由于GridView的分页功能实在是太弱了,所以需要使用强大的AspNetPager来作为分页控件。最简单的办法就是GridView控件下面接着放一个AspNetPager控件,但是这样好像就不能用GridView的分页功能了。在数据量不大的情况下,使用GridView的分页是十分方便有效的。另外还有一个问题就是分页控件在GridView生成的表格的下面,而没有像GridView自带分页那样包含到表格内部,这点也不是很爽。

要解决以上的问题,可以将AspNetPager放入GridView的分页模板(PagerTemplate)中,如下代码所示:

  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
  2.     <Columns>
  3.         <asp:BoundField DataField="DmId" HeaderText="序号" ReadOnly="True" SortExpression="DMID" />
  4.         ……
  5.         ……
  6.     </Columns>
  7.     <PagerStyle HorizontalAlign="Center" />
  8.     <PagerTemplate>
  9.         <asp:AspNetPager ID="AspNetPager1" runat="server" ShowBoxThreshold="5" ShowPageIndexBox="Auto" CenterCurrentPageButton="True">
  10.         </asp:AspNetPager>
  11.     </PagerTemplate>
  12. </asp:GridView>

(1)这个GridView每一页的行数AspNetPager并不知道。解决办法:为AspNetPager添加属性PageSize="<%# ((GridView)Container.NamingContainer).PageSize%>"

(2)这个GridView绑定的总记录数AspNetPager也不知道。解决办法:为AspNetPager添加属性RecordCount="<%#((IList)(((GridView)Container.NamingContainer).DataSource)).Count %>"

(3)这个GridView当前在第几页AspNetPager也不知道。这个问题的解决可不像前面那么简单了,通过设置属性CurrentPageIndex的方式AspNetPager根本不认!(估计是AspNetPager的一个Bug吧)要解决这个问题就只有在每次翻页时后台代码中为AspNetPager设置CurrentPageIndex属性。

(4)使用AspNetPager后GridView并不会触发PageChanging事件。但是要触发AspNetPager的PageChanging事件,所以可以为分页模板中的AspNetPager控件添加事件处理:OnPageChanging="AspNetPager1_PageChanging",对应的就是分页的后台代码:

protected   void  AspNetPager1_PageChanging( object  src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
    
this .GridView1.PageIndex  =  e.NewPageIndex  -   1 ;
// 这儿需要注意,AspNetPager中的PageIndex是从1开始的,而GridView的是从0开始的,所以要减1.
    Bind(); // GridView的数据绑定方法
    Wuqi.Webdiyer.AspNetPager pager  =   this .GridView1.BottomPagerRow.FindControl( " AspNetPager1 " as  Wuqi.Webdiyer.AspNetPager;
    pager.CurrentPageIndex 
=  e.NewPageIndex; // 这里就是为了解决前面的第3个问题。
}

OK,以上4个问题都解决了,我们的GridView+AspNetPager的分页就完成了!另外如果觉得AspNetPager的样式不好看可以再定义一下CSS。最后完整的代码是:

  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True">
  2.     <Columns>
  3.         <asp:BoundField DataField="DmId" HeaderText="序号" ReadOnly="True" SortExpression="DMID" />
  4.         ……
  5.         ……
  6.     </Columns>
  7. <PagerStyle CssClass="GridViewPager" HorizontalAlign="Center" />
  8.     <PagerTemplate>
  9.         <asp:AspNetPager ID="AspNetPager1" runat="server" ShowBoxThreshold="5" ShowPageIndexBox="Auto"
  10.         PageSize="<%# ((GridView)Container.NamingContainer).PageSize%>" OnPageChanging="AspNetPager1_PageChanging"
  11.         RecordCount="<%#((IList)(((GridView)Container.NamingContainer).DataSource)).Count %>"
  12.         CenterCurrentPageButton="True">
  13.         </asp:AspNetPager>
  14.     </PagerTemplate>
  15. </asp:GridView>
后台代码
  1. protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
  2. {
  3.     this.GridView1.PageIndex = e.NewPageIndex - 1;
  4.     Bind();
  5.     Wuqi.Webdiyer.AspNetPager pager = this.GridView1.BottomPagerRow.FindControl("AspNetPager1"as Wuqi.Webdiyer.AspNetPager;
  6.     pager.CurrentPageIndex = e.NewPageIndex;
  7. }

1.将AspNetPager控件放入工具箱的方法是右键点击工具箱,选择添加项目,然后刘览相关dll文件。

2.控件外观的设定

   <webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="true" PageSize="5" ShowCustomInfoSection="Left" NumericButtonTextFormatString="[{0}]" ShowBoxThreshold="5" AlwaysShow="true" OnPageChanged="AspNetPager1_PageChanged" >

   </webdiyer:AspNetPager>

ShowCustomInfoSection大约是一个安放自定义文本的东东。PageSize设定分页显示的记录笔数。OnPageChanged事件调用后台的方法。

//显示记录信息

        AspNetPager1.CustomInfoText = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";

        AspNetPager1.CustomInfoText += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";

        AspNetPager1.CustomInfoText += " 当前页:<font color=/"red/"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值