1.在vs web应用程序项目里引用AspNetPager.dll,在工具箱中添加AspNetPager控件。
2.在aspx里面拖入AspNetPager控件,设定分页控件每页显示条目数PageSize。
例:
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
UrlPaging="true"
FirstPageText="首页" PrevPageText="上一页" NextPageText="下一页" LastPageText="尾页"
AlwaysShow="True" onpagechanged="AspNetPager1_PageChanged" PageSize="2"
CurrentPageButtonStyle="whitefont" PageIndexBoxType="DropDownList"
ShowPageIndexBox="Always">
</webdiyer:AspNetPager>
拖入控件后会在网页头部添加如下代码:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
3.在aspx.cs文件Page_Load方法里设置AspNetPager控件的RecordCount。
例:通过连接数据库获取需要显示信息的条目数
1 if (!this.IsPostBack) //判断网页首次载入
2 {
3 int recordCount = int.Parse(SqlConnectOpr.ExecuteScalar("SELECT COUNT(*) FROM MessageType", null).ToString()); //链接数据库获取条目数
4 this.AspNetPager1.RecordCount = recordCount; //设定ID为AspNetPager1的分页控件的RecordCount属性值,即总条目数。
5 }
4.设定分页控件的urlPaging为true。
5.修改分页控件的PageChanged函数。
1 protected void AspNetPager1_PageChanged(object sender, EventArgs e)
2 {
3 int currentPage = 1; //默认显示第一页。
4 if (!string.IsNullOrEmpty(Request.QueryString["page"]))
5 {
6 currentPage = int.Parse(Request.QueryString["page"]);
7 } //通过网页get方式获取当前页码。
8 this.DataBindToPage(2, currentPage); //链接数据库并显示需要显示的数据条目,2为每页显示条目数。
9 }
来源:https://www.cnblogs.com/Leroy1245/archive/2012/08/09/2630365.html