在一些数据量不是很多的时候,自己往往不想写分页的方法,因为有时候在读取每个页面数据集合时都要去写分页的方法,然后在每个页面又得写分页的处理事件,
所以这样会出现代码冗余,基于这种情况我在项目中自己试着去写一个分页的用户控件,每个页面只要用到了分页,只要直接把这个控件拖过来就是了。嗯废话不多说
那首先来看下我的代码:
首先是分页控件的前台代码:
<%@ Control Language="C#" AutoEventWireup="true" Inherits="AutoPage" Codebehind="AutoPage.ascx.cs" %>
共<asp:Label id="lb_ItemCount" ForeColor="Red" runat="server"></asp:Label>条记录 <asp:LinkButton ID="hpl_First" runat="server" OnClick="hpl_First_Click">首页</asp:LinkButton> <asp:LinkButton ID="hpl_Prev" runat="server" OnClick="hpl_Prev_Click">上一页</asp:LinkButton>当前<asp:label id="lb_CurrentPage" runat="server" ForeColor="Red"></asp:label>页/共<asp:label id="lb_PageCount" runat="server" ForeColor="Red"></asp:label>页<asp:LinkButton ID="hpl_Next" runat="server" OnClick="hpl_Next_Click">下一页</asp:LinkButton> <asp:LinkButton ID="hpl_Last" runat="server" OnClick="hpl_Last_Click">末页</asp:LinkButton>跳转到<asp:DropDownList ID="ddlPage" runat="server" AutoPostBack="True" CssClass="DropDownListCss" OnSelectedIndexChanged="ddlPage_SelectedIndexChanged"></asp:DropDownList>页 效果如图:
后台代码:
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls; //声明委托public delegate void DeleteAllHandler();public delegate void PaperChangeHandler();public partial class AutoPage : System.Web.UI.UserControl{ //是否显示选择和删除按钮 private bool showButton=false ; public bool ShowButton { get { return showButton; } set { showButton = value; } } //页面大小 private int pagesize; public int Pagesize { get { return pagesize; } set { pagesize = value; } } //设置当前页的索引 private int curPage=1;public int CurPage
{ get { return curPage; } set { curPage = value; } } string remark=""; //声明事件 public event DeleteAllHandler DeleteAllEvent; public event PaperChangeHandler PaperChange;protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { }}
public PagedDataSource databind(DataSet ds)
{ //创建分页类 PagedDataSource objPage = new PagedDataSource(); //设置数据源 objPage.DataSource = ds.Tables[0].DefaultView; //允许分页 objPage.AllowPaging = true; //设置每页显示的项数 objPage.PageSize = pagesize; //如用户点击的不是搜索按钮 if (remark == "1") { if (ViewState["CurPage"] != null) { CurPage = Convert.ToInt32(ViewState["CurPage"].ToString()); } } else { ViewState["CurPage"] = curPage;}
remark = ""; if (CurPage < 1) { CurPage = 1; } if (CurPage > objPage.PageCount) { CurPage= int.Parse(objPage.PageCount.ToString()); } objPage.CurrentPageIndex = CurPage - 1;//显示状态信息
lb_ItemCount.Text = ds.Tables[0].Rows.Count.ToString(); lb_CurrentPage.Text = CurPage.ToString(); lb_PageCount.Text = objPage.PageCount.ToString(); ViewState["totelPage"] = int.Parse(objPage.PageCount.ToString()); //绑定下拉框 this.ddlPage.Items.Clear(); for (int i = 1; i < objPage.PageCount + 1; i++) { this.ddlPage.Items.Add(i.ToString()); } this.ddlPage.Text = CurPage.ToString(); hpl_Prev.Enabled = true; hpl_First.Enabled = true; hpl_Next.Enabled = true; hpl_Last.Enabled = true; //如果当前页面等于首页 if (CurPage.ToString() == "1" ) { hpl_Prev.Enabled = false; hpl_First.Enabled = false; }//如果当前页面等于末页
if (CurPage.ToString() == objPage.PageCount.ToString()) { hpl_Next.Enabled = false; hpl_Last.Enabled = false; } //如果只有一页则隐藏分页控件 if (showButton == false) { if (objPage.PageCount == 1) { this.Visible = false; } else { this.Visible = true; } } else { if (ds.Tables[0].Rows.Count.ToString() == "0") { this.Visible = false; } else { this.Visible = true; } } return objPage; } protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e) { if (PaperChange != null) { ViewState["CurPage"] = ddlPage.Text; remark = "1"; this.PaperChange(); } }protected void lbtnDeleteSelectAll_Click(object sender, EventArgs e) { if (this.DeleteAllEvent != null) { this.DeleteAllEvent(); } }
//首页
protected void hpl_First_Click(object sender, EventArgs e) { if (PaperChange != null) { ViewState["CurPage"] = "1"; remark = "1"; this.PaperChange(); } }//上一页
protected void hpl_Prev_Click(object sender, EventArgs e) { if (PaperChange != null) { ViewState["CurPage"] = int.Parse(ViewState["CurPage"].ToString()) - 1; remark = "1"; this.PaperChange(); } }//下一页
protected void hpl_Next_Click(object sender, EventArgs e) {if (PaperChange != null)
{ ViewState["CurPage"] = int.Parse(ViewState["CurPage"].ToString()) + 1; remark = "1"; this.PaperChange(); } } //末页 protected void hpl_Last_Click(object sender, EventArgs e) { if (PaperChange != null) { ViewState["CurPage"] = int.Parse(ViewState["totelPage"].ToString()); remark = "1"; this.PaperChange(); } }}一个分页控件就这样写好了 如果大家在做一个数据量不是很大的项目仅有上万条数据的时候完全可以用我写的这个用户控件,这样可以大大的节约时间。
然后大家看了分页控件 于是又会问 我要怎么去它了 这我告诉大家 其实用它非常简单 只要把用户控件拖到你要分页的页面上就可以了
看下我的代码
<uc1:AutoPage ID="AutoPage1" runat="server" Pagesize="10" ShowButton="true" />
拖过来以后 只要指定pagesize 和ShowButton="true"属性就可以了。
当然在后置代码里:
需要定义一个委托事件
//委托
this.AutoPage1.PaperChange += new PaperChangeHandler(AutoPage1_PaperChange);AutoPage1_PaperChange这个是一个绑定的方法哦!
如下:
private void AutoPage1_PaperChange()
{ BandList(); }那这个BandList()方法又是怎么写的了?
代码如下:
this.GridView1.DataSource = AutoPage1.databind(per.PersonDs);
this.GridView1.DataBind();per.PersonDs这是一个DataSet集合 所以你只要 AutoPage1.databind("这里传一个DataSet就可以了");
呵呵...
以上这个控件就是我写的啦 当然代码还有待改进 麻烦高人指点 谢谢了....