不同于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的值
/// Index 的摘要说明。
///
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=”记录总数: “+pager.RecordCount.ToString()+”“;
pager.CustomInfoText+=” 总页数: “+pager.PageCount.ToString()+”“;
pager.CustomInfoText+=” 当前页: “+pager.CurrentPageIndex.ToString()+”“;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
///
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(”