ASP.Net分页方法详解

1.添加DLL引用,文件名为:AspNetPager.dll。
2.在aspx文件中添加<%@ RegisterAssembly="AspNetPager" Namespace="Wuqi.Webdiyer"TagPrefix="webdiyer" %>代码,下面是分页样式代码:

 

3.在CS文件中写using Wuqi.Webdiyer;
4.获取数据源,下面是获得数据的一个方法,BLL.Trunk.GetAllList方法是一个封装过的方法,返回一个DataSet结果集,这个方法自己写就可以,这是来自项目中的代码:

 

View Code
1 private void LoadData()
2 {
3 DataSet ds =BLL.Trunk.GetAllList();
4 PagedDataSource pds = newPagedDataSource();
5 pds.AllowPaging =true;//设置允许分页
6 pds.DataSource =ds.Tables[0].DefaultView;//设置分页的数据源
7 AspNetPager1.RecordCount = pds.Count;//AspNetPager1.RecordCount =ds.Tables[0].DefaultView.Count;等价//获取数据的条数
8 pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex -1;//设置当前页的索引
9 pds.PageSize =AspNetPager1.PageSize;//设置每页显示的页数
10 gvData.DataSource = pds;
11 gvData.DataBind();
12 }

5.事件分页改变代码:



View Code
1 protected void AspNetPager1_PageChanging(object src,PageChangingEventArgs e)
2 {
3 this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex+ 1, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount,this.AspNetPager1.PageSize});
4 AspNetPager1.CurrentPageIndex =e.NewPageIndex;
5 LoadData();
6 }

View Code
1 <div id="PagerStyle">
2  <webdiyer:AspNetPager ID="AspNetPager1"runat="server" OnPageChanging="AspNetPager1_PageChanging"Width="95%" PageSize="20" AlwaysShow="True" FirstPageText="首页"LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"ShowCustomInfoSection="Left" ShowInputBox="Never"CustomInfoTextAlign="Right"CssClass="paginator"
3 CurrentPageButtonClass="cpb" CustomInfoHTML="当前第 %CurrentPageIndex%/ %PageCount% 共 %PageCount%页 每页 %PageSize% 条记录"AlwaysShowFirstLastPageNumber="True"LayoutType="Table">
4  </webdiyer:AspNetPager>
5  </div>
原文地址:https://www.cnblogs.com/wsl2011/p/2063517.html