using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ustonetdonet.DotNetPagedData
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 本类提供GridView,DataList,Repeater的通用分页
/// 参考了NetFire(Fire.Rolland.Han)http://netfire.my.gsdn.net/2005/04/04/2945/
///
///
/// 使用方法:
/// 1.在引用里添加dll的引用
/// 2.using ustonetdonet.DotNetPagedData;
/// 3.private DotNetPagedData pager;
/// 4.page_load()
/// if(!IsPostBack)
/// {
/// pager=new DotNetPagedData();
/// pager.PagedPanel=this.pagedPanel;
/// pager.GridView = this.GridView1; //如果是DataGrid则添加此项
/// pager.Datalist=this.DataList1; //如果是DataList则添加此项
/// pager.Repeater=this.Repeater1; //如果是Repeater则添加此项
/// pager.Lnknext=this.lnkNext;
/// pager.Lnkprev=this.lnkPrev;
/// pager.Lnklast=this.lnklast;
/// pager.Lnkfist=this.lnkfist;
/// pager.Pagenum=this.Pagenum;
/// pager.Pagecount=this.Pagecount;
/// pager.List=this.ddl_jump;
/// //简单示例定义DataSet外部定义,同样也可以采用执行sqlstr,并填充给DataSet,本类也重载了datapage(),详细信息可查看详细代码
/// DataSet ds=new DataSet();
/// ds.ReadXml(Server.MapPath("test.xml"));
/// pager.datapage(ds,5);
/// }
///
/// // DropDownList必须设定为AutoPostBack,下面添加它的事件
/// 5.private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
/// {
/// pager=new DotNetPagedData();
/// pager.List=this.DropDownList1;
/// pager.Lnkjump=this.HyperLink3;
/// pager.select();
/// }
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class DotNetPagedData : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
private System.Web.UI.WebControls.GridView gridview = 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.Panel pagedPanel;
private System.Web.UI.WebControls.DropDownList list;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DotNetPagedData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
//
// TODO: 在此处添加构造函数逻辑
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.GridView GridView
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.gridview;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.gridview = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.DataList Datalist
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.datalist;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.datalist = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.Repeater Repeater
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.repeater;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.repeater = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.HyperLink Lnkprev
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.lnkprev;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnkprev = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.HyperLink Lnknext
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.lnknext;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnknext = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.HyperLink Lnkfist
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.lnkfist;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnkfist = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.HyperLink Lnklast
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.lnklast;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnklast = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.HyperLink Lnkjump
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.lnkjump;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnkjump = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.Label Pagenum
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.pagenum;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.pagenum = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.Label Pagecount
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.pagecount;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.pagecount = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.DropDownList List
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.list;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.list = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public System.Web.UI.WebControls.Panel PagedPanel
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return this.pagedPanel;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.pagedPanel = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//针对一般数据邦定,不执行sqlstr
public void datapage(DataSet ds, int pagenum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.list.Items.Add(i.ToString());
}
this.list.Items.Insert(0, new ListItem("页数", ""));
//确定当前页数
if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CurPage = 1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
objPds.CurrentPageIndex = CurPage - 1;
if (objPds.PageCount > 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pagedPanel.Visible = true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pagedPanel.Visible = false;
}
//显示当前页数
this.pagenum.Text = "当前:" + CurPage.ToString() + "页";
//显示总共页数
this.pagecount.Text = "共有" + objPds.PageCount + "页";
//下一页
if (!objPds.IsLastPage)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
//上一页
if (!objPds.IsFirstPage)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
//第一页
if (CurPage != 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
}
//最后一页
if (CurPage != objPds.PageCount)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
}
//绑定gridview
if (this.gridview != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
gridview.DataSource = objPds;
gridview.DataBind();
}
//绑定datalist
if (this.datalist != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
datalist.DataSource = objPds;
datalist.DataBind();
}
//绑定repeater
if (this.repeater != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
repeater.DataSource = objPds;
repeater.DataBind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//重载,针对执行sqlstr
public void datapage(string sqlcon, string sqlstr, int pagenum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
SqlConnection objConn = new SqlConnection(sqlcon);
SqlDataAdapter objCommand = new SqlDataAdapter(sqlstr, objConn);
DataSet ds = new DataSet();
objCommand.Fill(ds);
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++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.list.Items.Add(i.ToString());
}
this.list.Items.Insert(0, new ListItem("页数", ""));
//确定当前页数
if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CurPage = 1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
objPds.CurrentPageIndex = CurPage - 1;
if (objPds.PageCount > 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pagedPanel.Visible = true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pagedPanel.Visible = false;
}
//显示当前页数
this.pagenum.Text = "当前:" + "第" + CurPage.ToString() + "页";
//显示总共页数
this.pagecount.Text = "共有" + objPds.PageCount + "页";
//下一页
if (!objPds.IsLastPage)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
//上一页
if (!objPds.IsFirstPage)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
//第一页
if (CurPage != 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
}
//最后一页
if (CurPage != objPds.PageCount)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
}
//绑定gridview
if (this.gridview != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
gridview.DataSource = objPds;
gridview.DataBind();
}
//绑定datalist
if (this.datalist != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
datalist.DataSource = objPds;
datalist.DataBind();
}
//绑定repeater
if (this.repeater != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
repeater.DataSource = objPds;
repeater.DataBind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//dropdownlist选择改变
public void select()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.lnkjump.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(this.list.SelectedItem.Value);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
|