数据分页一直以来是比较头疼的问题,不少的分页控件也随之产生。但是我用过的几个控件,本身都要执行SQL,对存储过程支持不好,为了方便,特别写了个通用分页类,利用PagedDataSource来实现DataGrid,DataList,Repeater的分页。完成上一页,下一页, 最后一页,最前一页,总页数,当前页数,跳转这些基本的功能。
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace aspnetpager { /// /// netpager-本类提供datagrid,datalist,repeater的通用分页---jierry 04-7-19 /// public class netpager: System.Web.UI.Page { private System.Web.UI.WebControls.DataGrid datagrid=null; private System.Web.UI.WebControls.DataList datalist=null; private System.Web.UI.WebControls.Repeater repeater=null; private System.Web.UI.WebControls.HyperLink lnknext; private System.Web.UI.WebControls.HyperLink lnkprev; private System.Web.UI.WebControls.HyperLink lnkfist; private System.Web.UI.WebControls.HyperLink lnklast; private System.Web.UI.WebControls.HyperLink lnkjump; private System.Web.UI.WebControls.Label pagenum; private System.Web.UI.WebControls.Label pagecount; private System.Web.UI.WebControls.DropDownList list; public netpager() { // // TODO: 在此处添加构造函数逻辑 // }
public System.Web.UI.WebControls.DataGrid Datagrid { get { return this.datagrid; } set { this.datagrid=value; } }
public System.Web.UI.WebControls.DataList Datalist { get { return this.datalist; } set { this.datalist=value; } }
public System.Web.UI.WebControls.Repeater Repeater { get { return this.repeater; } set { this.repeater=value; } }
public System.Web.UI.WebControls.HyperLink Lnkprev { get { return this.lnkprev; } set { this.lnkprev=value; } }
public System.Web.UI.WebControls.HyperLink Lnknext { get { return this.lnknext; } set { this.lnknext=value; } }
public System.Web.UI.WebControls.HyperLink Lnkfist { get { return this.lnkfist; } set { this.lnkfist=value; } }
public System.Web.UI.WebControls.HyperLink Lnklast { get { return this.lnklast; } set { this.lnklast=value; } }
public System.Web.UI.WebControls.HyperLink Lnkjump { get { return this.lnkjump; } set { this.lnkjump=value; } }
public System.Web.UI.WebControls.Label Pagenum { get { return this.pagenum; } set { this.pagenum=value; } }
public System.Web.UI.WebControls.Label Pagecount { get { return this.pagecount; } set { this.pagecount=value; } }
public System.Web.UI.WebControls.DropDownList List { get { return this.list; } set { this.list=value; } }
public void datapage(DataSet ds,int pagenum) { PagedDataSource objPds = new PagedDataSource(); objPds.AllowPaging =true; //每页显示数目 objPds.PageSize =pagenum; //指定数据源 objPds.DataSource = ds.Tables[0].DefaultView; int CurPage; //绑定dropdownlist数据 for(int i=1;i<=objPds.PageCount;i++) { this.list.Items.Add(i.ToString()); } this.list.Items.Insert(0,new ListItem("页数","")); //确定当前页数 if (System.Web.HttpContext.Current.Request.Params["Page"] != null) { CurPage=Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]); } else { CurPage=1; }
objPds.CurrentPageIndex = CurPage-1; //显示当前页数 this.pagenum.Text="页数:"+CurPage.ToString(); //显示总共页数 this.pagecount.Text="共有"+objPds.PageCount+"页";
//下一页 if (!objPds.IsLastPage) { lnknext.NavigateUrl=System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"?Page=" + Convert.ToString(CurPage+1); } //上一页 if(!objPds.IsFirstPage) { lnkprev.NavigateUrl=System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(CurPage-1); } //第一页 if(CurPage!=1) { lnkfist.NavigateUrl=System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(1); } //最后一页 if(CurPage!=objPds.PageCount) { lnklast.NavigateUrl=System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(objPds.PageCount); } //绑定datagrid if(this.datagrid!=null) { datagrid.DataSource=objPds; datagrid.DataBind(); } //绑定datalist if(this.datalist!=null) { datalist.DataSource=objPds; datalist.DataBind(); } //绑定repeater if(this.repeater!=null) { repeater.DataSource=objPds; repeater.DataBind(); } }
//dropdownlist选择改变 public void select() { this.lnkjump.NavigateUrl=System.Web.HttpContext.Current.Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(this.list.SelectedItem.Value); }
} }
使用方法: 1.在引用里添加aspnetpager.dll的引用 2.using aspnetpager; 3.private netpager pager; 4. page_load() if(!IsPostBack) { pager=new netpager(); pager.Datagrid=this.DataGrid1; pager.Datalist=this.DataList1; pager.Repeater=this.Repeater1; pager.Lnknext=this.HyperLink1; pager.Lnkprev=this.lnkprev; pager.Lnklast=this.lnklast; pager.Lnkfist=this.lnkfist; pager.Pagenum=this.Label1; pager.Pagecount=this.Label2; pager.List=this.DropDownList1; DataSet ds=new DataSet(); ds.ReadXml(Server.MapPath("test.xml")); pager.datapage(ds,5); } 5.private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { pager=new netpager(); pager.List=this.DropDownList1; pager.Lnkjump=this.HyperLink3; pager.select(); }
|