不同于DataGrid控件,AspNetPager分页控件本身并不显示任何数据,而只显示页导航元素,数据在页面上的显示方式与该控件无关。该控件可以为DataGrid、DataList、Repeater以及自定义控件进行分页,配合Sql存储过程,分页性能较使用DataGrid分页有明显提升,尤其是当数据量大时性能可提升数倍!
AspNetPager 2.0 中新增了通过Url来分页的功能,这使得访问者可以直接输入相应的Url来访问任何页面,并且搜索引擎也可以直接检索每个页面,若使用DataGrid的分页功能,这是无法实现的。
要使用 AspNetPager 分页控件,必须最少指定它的 RecordCount 属性,指定并编写 PageChanged 事件的处理程序。 RecordCount 属性指定要分页的所有数据的总项数,若未指定该值或该值小于等于 PageSize ,则AspNetPager控件不会显示任何内容。 若未指定并编写 PageChanged 事件处理程序,则当用户点击页导航元素或在页索引文本框中手式输入页索引并提交时AspNetPager不会跳转到指定的页。 AspNetPager控件的分页方法和DataGrid基本相同,即在它的 PageChanged 事件处理程序中将传递事件数据的 PageChangedEventArgs 的 NewPageIndex值赋给 AspNetPager的 CurrentPageIndex属性,然后重新将新的数据与数据显示控件绑定。
RecordCount :当页面第一次加载时,应以编程方式将从存储过程或Sql语句中返回的数据表中所有要分页的记录的总数赋予该属性,AspNetPager会将其保存的ViewState中并在页面回发时从ViewState中获取该值,因此避免了每次分页都要访问数据库而影响分页性能。AspNetPager根据要分页的所有数据的总项数和 PageSize 属性来计算显示所有数据需要的总页数,即 PageCount的值。
PageSize :该值获取或设置数据呈现控件每次要显示数据表中的的数据的项数,AspNetPager根据该值和 RecordCount 来计算显示所有数据需要的总页数,即 PageCount的值
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace ShowPage
{
/// <summary>
/// Index 的摘要说明。
/// </summary>
public class Index : System.Web.UI.Page
{
protected Wuqi.Webdiyer.AspNetPager pager;
protected System.Web.UI.WebControls.DataGrid dgList;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConn);
string strsql="Select count(id) from article";
SqlCommand myCommand = new SqlCommand(strsql,myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda =new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds = new DataSet();
myda.Fill(ds,"article");
this.pager.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);
BindData();
}
}
void BindData()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConn);
string strsql="Select id,topic from article";
SqlCommand myCommand = new SqlCommand(strsql,myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda =new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds=new DataSet();
myda.Fill(ds,pager.PageSize*(pager.CurrentPageIndex-1),pager.PageSize,"article");
this.dgList.DataSource=ds.Tables["article"];
this.dgList.DataBind();
//动态设置用户自定义文本内容
pager.CustomInfoText="记录总数:<font color=/"blue/"><b>"+pager.RecordCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 总页数:<font color=/"blue/"><b>"+pager.PageCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 当前页:<font color=/"red/"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pager.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.pager_PageChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
pager.CurrentPageIndex=e.NewPageIndex;
BindData();
System.Text.StringBuilder sb=new StringBuilder("<script Language=/"Javascript/"><!--/n");
sb.Append("var el=document.all;");
sb.Append(this.dgList.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
if(!Page.IsStartupScriptRegistered("scrollScript"))
Page.RegisterStartupScript("scrollScript",sb.ToString());
}
}
}
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>
<%@Register TagPrefix="Webdiyer" Namespace="Wuqi.Webdiyer" Assembly="aspnetpager"%>
<HTML>
<HEAD>
<TITLE>Welcome to Webdiyer.com </TITLE>
<script runat="server">
SqlConnection conn;
SqlCommand cmd;
void Page_Load(object src,EventArgs e)
{
conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
if(!Page.IsPostBack)
{
cmd=new SqlCommand("GetNews",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",1);
cmd.Parameters.Add("@pagesize",1);
cmd.Parameters.Add("@docount",true);
conn.Open();
pager.RecordCount=(int)cmd.ExecuteScalar();
conn.Close();
BindData();
}
}
void BindData()
{
cmd=new SqlCommand("GetNews",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",pager.CurrentPageIndex);
cmd.Parameters.Add("@pagesize",pager.PageSize);
cmd.Parameters.Add("@docount",false);
conn.Open();
dataGrid1.DataSource=cmd.ExecuteReader();
dataGrid1.DataBind();
conn.Close();
pager.CustomInfoText="记录总数:<font color=/"blue/"><b>"+pager.RecordCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 总页数:<font color=/"blue/"><b>"+pager.PageCount.ToString()+"</b></font>";
pager.CustomInfoText+=" 当前页:<font color=/"red/"><b>"+pager.CurrentPageIndex.ToString()+"</b></font>";
}
void ChangePage(object src,PageChangedEventArgs e)
{
pager.CurrentPageIndex=e.NewPageIndex;
BindData();
}
</script>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer(yhaili@21cn.com)">
</HEAD>
<body>
<form runat="server" ID="Form1">
<asp:DataGrid id="dataGrid1" runat="server" />
<Webdiyer:AspNetPager id="pager"
runat="server"
PageSize="8"
NumericButtonCount="8"
ShowCustomInfoSection="left"
PagingButtonSpacing="0"
ShowInputBox="always"
CssClass="mypager"
HorizontalAlign="right"
OnPageChanged="ChangePage"
SubmitButtonText="转到"
NumericButtonTextFormatString="[{0}]"/>
</form>
</body>
</HTML>