自己写的一个ASP.NET服务器控件Repeater和GridView分页类

时间:2020-12-23 03:41:40

不墨迹,直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; namespace CutPage
{
//定义查询方法的委托。pageindex查询的页码;pagesize每一页的数据量
public delegate object GetList(int pageindex,int pagesize);
public class SplitPage
{
public int CurrentPageIndex
{
get
{
return Convert.ToInt32(CurrentPage.Session["index"]);
}
set { } }//当前页码
public int PageCount {get;set; }//页面的数量
public event GetList GetListHandle;//根据委托定义查询事件
public HtmlGenericControl HGC { get; set; }//容器(div)
public int PageSize { get; set; }//每个页面中数据的数量
public Page CurrentPage { get; set; }//当前页面
public Repeater ReControl { get; set; }
public GridView GvControl { get; set; } public LinkButton UpLb = new LinkButton {
Text=" 上一页 ",ID="uplb"
};//上一页按钮
private LinkButton DownLb = new LinkButton { Text=" 下一页 ",ID="downlb"};//下一页按钮 private LinkButton DUpLb = new LinkButton { Text=" ... ",ID="DUpLb"};
private LinkButton DDownLb = new LinkButton { Text=" ... ",ID="DDownLb"}; private TextBox txtPage = new TextBox { ID = "txtPage" };
private LinkButton lbGo = new LinkButton { Text=" Go ",ID="lbGo"}; //构造方法,传入当前页面
public SplitPage(Page page)
{ PageSize = ;
CurrentPage = page;
HGC = new HtmlGenericControl(); //判断单签页面的session中是否有index这个项,如果没有则加入其实为0
if (CurrentPage.Session["index"] == null)
CurrentPage.Session.Add("index", );
} //绑定Repeater控件方法,返回一个html容器(div)
public HtmlGenericControl BindRepeater(Repeater re)
{ Procee();
ReControl = re;
re.DataSource = GetListHandle(CurrentPageIndex, PageSize);
re.DataBind();
return HGC;
} public HtmlGenericControl BindGridView(GridView gv)
{
Procee();
this.GvControl = gv;
gv.DataSource = GetListHandle(CurrentPageIndex,PageSize);
gv.DataBind();
return HGC;
} private void Procee()
{
CreateControl();
UpLb.Click += UpLb_Click;
DownLb.Click += DownLb_Click;
HGC.Controls.Add(UpLb);
HGC.Controls.Add(DownLb); HGC.Controls.Add(txtPage);
lbGo.Click += lbGo_Click;
HGC.Controls.Add(lbGo);
} //根据当前页码生成控件
public void CreateControl()
{ int pages = CurrentPageIndex / * ;
this.HGC.Controls.Clear();
if (pages > )
{
DUpLb.Click += DUpLb_Click;
this.HGC.Controls.Add(DUpLb);
}
for (int i = pages; i < pages + ; i++)
{
if (i <= PageCount)
{
LinkButton lb = new LinkButton();
if (i == CurrentPageIndex) lb.Enabled = false;
lb.Text = " " + i.ToString();
lb.ID = i.ToString();
lb.Click += lb_Click;
this.HGC.Controls.Add(lb);
}
}
if (pages+ <= this.PageCount)
{
DDownLb.Click += DDownLb_Click;
this.HGC.Controls.Add(DDownLb);
} } private void lbGo_Click(object sender, EventArgs e)
{
int goIndex = ;
if (Int32.TryParse(txtPage.Text,out goIndex))
{
if (goIndex <= PageCount&&goIndex>)
{
CurrentPage.Session["index"] = goIndex;
CreateControl();
IsNull();
}
else
CurrentPage.Response.
Write("<script>alert('索引超出范围')</script>"); }
else
CurrentPage.Response.
Write("<script>alert('请输入正确的索引')</script>"); } //页码的单击事件
private void lb_Click(object sender, EventArgs e)
{
LinkButton lb = sender as LinkButton;
CurrentPage.Session["index"]=Convert.ToInt32(lb.Text);
CreateControl();
IsNull(); } //判断是绑定的控件
private void IsNull()
{
if (ReControl != null)
BindRepeater(ReControl);
else
BindGridView(GvControl);
} //上一页的单击事件
private void UpLb_Click(object sender, EventArgs e)
{
if (CurrentPageIndex >= )
{
CurrentPage.Session["index"] = --CurrentPageIndex;
CreateControl();
IsNull();
}
} //下一页的单击事件
private void DownLb_Click(object sender, EventArgs e)
{
if (CurrentPageIndex < PageCount)
{
CurrentPage.Session["index"] = ++CurrentPageIndex;
CreateControl();
IsNull();
}
} private void DUpLb_Click(object sender, EventArgs e)
{
int pi = (CurrentPageIndex-) / * ;
CurrentPage.Session["index"] = pi;
IsNull();
} private void DDownLb_Click(object sender, EventArgs e)
{
int pi = (CurrentPageIndex + ) / * ;
CurrentPage.Session["index"] = pi;
IsNull();
} }
}

使用方法

            SplitPage sp = new SplitPage(this);
sp.GetListHandle += bm.GetBooks;//分页查询数据库的方法
sp.PageCount = bm.BooksCount() / sp.PageSize ;//总页数
this.divOne.Controls.Add(sp.BindGridView(dvBookInfo));//把放回的div加入到设置好的divOne中,dvBookInfo是一个GridView控件,
//也可以是Repeater控件

上面这个分页类可以在类中添加相关代码,支持其他ASP.NET服务器控件绑定数据后的分页

备注:菜鸟,求指点。