ASP.NET GridView,DataList,Repeater的通用分(fēn)頁 (C#)

發布時間:2007年10月17日      浏覽次數:3602 次
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;
namespace ustonetdonet.DotNetPagedData
{
/**//// <summary>
/// 本類提供GridView,DataList,Repeater的通用分(fēn)頁
/// 參考了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外(wài)部定義,同樣也可以采用執行sqlstr,并填充給DataSet,本類也重載了datapage(),詳細信息可查看詳細代碼
/// DataSet ds=new DataSet();
/// ds.ReadXml(Server.MapPath("test.xml"));
/// pager.datapage(ds,5);
/// }
///
/// // DropDownList必須設定爲AutoPostBack,下(xià)面添加它的事件
/// 5.private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
/// {
/// pager=new DotNetPagedData();
/// pager.List=this.DropDownList1;
/// pager.Lnkjump=this.HyperLink3;
/// pager.select();
/// }
/// </summary>
public class DotNetPagedData : System.Web.UI.Page
{
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;
public DotNetPagedData()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public System.Web.UI.WebControls.GridView GridView
{
get
{
return this.gridview;
}
set
{
this.gridview = 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 System.Web.UI.WebControls.Panel PagedPanel
{
get
{
return this.pagedPanel;
}
set
{
this.pagedPanel = value;
}
}
//針對一(yī)般數據邦定,不執行sqlstr
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;
if (objPds.PageCount > 1)
{
pagedPanel.Visible = true;
}
else
{
pagedPanel.Visible = false;
}
//顯示當前頁數
this.pagenum.Text = "當前:" + CurPage.ToString() + "頁";
//顯示總共頁數
this.pagecount.Text = "共有" + objPds.PageCount + "頁";
//下(xià)一(yī)頁
if (!objPds.IsLastPage)
{
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
//上一(yī)頁
if (!objPds.IsFirstPage)
{
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
//第一(yī)頁
if (CurPage != 1)
{
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
}
//最後一(yī)頁
if (CurPage != objPds.PageCount)
{
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
}
//綁定gridview
if (this.gridview != null)
{
gridview.DataSource = objPds;
gridview.DataBind();
}
//綁定datalist
if (this.datalist != null)
{
datalist.DataSource = objPds;
datalist.DataBind();
}
//綁定repeater
if (this.repeater != null)
{
repeater.DataSource = objPds;
repeater.DataBind();
}
}
//重載,針對執行sqlstr
public void datapage(string sqlcon, string sqlstr, int pagenum)
{
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++)
{
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;
if (objPds.PageCount > 1)
{
pagedPanel.Visible = true;
}
else
{
pagedPanel.Visible = false;
}
//顯示當前頁數
this.pagenum.Text = "當前:" + "第" + CurPage.ToString() + "頁";
//顯示總共頁數
this.pagecount.Text = "共有" + objPds.PageCount + "頁";
//下(xià)一(yī)頁
if (!objPds.IsLastPage)
{
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
//上一(yī)頁
if (!objPds.IsFirstPage)
{
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
//第一(yī)頁
if (CurPage != 1)
{
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
}
//最後一(yī)頁
if (CurPage != objPds.PageCount)
{
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
}
//綁定gridview
if (this.gridview != null)
{
gridview.DataSource = objPds;
gridview.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);
}
}
}
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!