写的不算好,仅供参考。
GridPager.ascx:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="GridPager.ascx.cs" Inherits="Test.BaseClass.GridPager" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%><?xml:namespace prefix = asp /><asp:linkbutton id=lbtnPre style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="..." Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage1 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="1" Visible="False" Enabled="False" Font-Size="Small">1</asp:linkbutton> <asp:linkbutton id=lbtnPage2 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="2" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage3 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="3" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage4 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="4" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage5 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="5" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage6 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="6" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage7 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="7" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage8 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="8" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage9 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="9" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnPage10 style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="10" Visible="False"></asp:linkbutton><asp:linkbutton id=lbtnNext style="BORDER-RIGHT: white 2px solid; BORDER-LEFT: white 1px solid" runat="server" Text="..." Visible="False"></asp:linkbutton>
///////////////////////////////////////////////////////////////////////////////////////
GridPager.ascx.cs:
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
namespace Test.BaseClass
{
public class GridPager : System.Web.UI.UserControl
{
#region 类变量
protected System.Web.UI.WebControls.LinkButton lbtnNext;
protected System.Web.UI.WebControls.LinkButton lbtnPage10;
protected System.Web.UI.WebControls.LinkButton lbtnPage9;
protected System.Web.UI.WebControls.LinkButton lbtnPage8;
protected System.Web.UI.WebControls.LinkButton lbtnPage7;
protected System.Web.UI.WebControls.LinkButton lbtnPage6;
protected System.Web.UI.WebControls.LinkButton lbtnPage5;
protected System.Web.UI.WebControls.LinkButton lbtnPage4;
protected System.Web.UI.WebControls.LinkButton lbtnPage3;
protected System.Web.UI.WebControls.LinkButton lbtnPage2;
protected System.Web.UI.WebControls.LinkButton lbtnPage1;
protected System.Web.UI.WebControls.LinkButton lbtnPre;
#endregion
#region
/// <summary>
/// 编号:01
/// 内容摘要:翻页的对象:DataGrid控件。
/// </summary>
public DataGrid TheGrid;
/// <summary>
/// 编号:02
/// 内容摘要:显示查询结果记录数的标签,可以不设置,表示无需显示。
/// </summary>
public Label LabelRowsCount;
/// <summary>
/// 编号:03
/// 内容摘要:显示查询结果总页数的标签,可以不设置,表示无需显示。
/// </summary>
public Label LabelPageCount;
/// <summary>
/// 编号:04
/// 内容摘要:显示当前页码的标签,可以不设置,表示无需显示。
/// </summary>
public Label LabelPageIndex;
/// <summary>
/// 编号:05
/// 内容摘要:查询数据的SQL语句。
/// </summary>
private string SelectSQL
{
get
{
if(ViewState[this.ClientID+"_SelectSQL"] != null)
return ViewState[this.ClientID+"_SelectSQL"].ToString();
else
return string.Empty;
}
set
{
ViewState[this.ClientID+"_SelectSQL"] = value;
}
}//SelectSQL结束
/// <summary>
/// 编号:06
/// 内容摘要:每页显示的行数。
/// </summary>
public int PageSizes
{
get
{
if(ViewState[this.ClientID+"_PageSizes"] != null)
return (int)ViewState[this.ClientID+"_PageSizes"];
else
return 20;
}
set
{
ViewState[this.ClientID+"_PageSizes"] = value;
}
}//PageSizes结束
/// <summary>
/// 编号:07
/// 内容摘要:总页数。
/// </summary>
private int PageCount
{
get
{
if(ViewState[this.ClientID+"_PageCount"] != null)
return (int)ViewState[this.ClientID+"_PageCount"];
else
return 0;
}
set
{
ViewState[this.ClientID+"_PageCount"] = value;
}
}//PageCount结束
/// <summary>
/// 编号:08
/// 内容摘要:当前页码。
/// </summary>
private int CurrentPageIndex
{
get
{
if(ViewState[this.ClientID+"_CurrentPageIndex"] != null)
return (int)ViewState[this.ClientID+"_CurrentPageIndex"];
else
return 0;
}
set
{
ViewState[this.ClientID+"_CurrentPageIndex"] = value;
if(this.LabelPageIndex!= null)
this.LabelPageIndex.Text = (value+1).ToString();
}
}//CurrentPageIndex结束
private LinkButton[] PageButtons
{
get
{
return new LinkButton[]{lbtnPage1,lbtnPage2,lbtnPage3,lbtnPage4,lbtnPage5,
lbtnPage6,lbtnPage7,lbtnPage8,lbtnPage9,lbtnPage10};
}
}
#endregion
#region 方法区开始
/// <summary>
/// 方法编号:01
/// 内容摘要:初始化页面。
/// </summary>
private void Page_Load(object sender, System.EventArgs e)
{
}
/// <summary>
/// 方法编号:02
/// 内容摘要:设置与本控件关联的控件。
/// </summary>
/// <param name="mDataGrid">DataGrid控件,必须提供</param>
/// <param name="lblRowsCount">显示查询结果记录数的标签,可以输入null,表示无需显示</param>
/// <param name="lblPageCount">显示查询结果总页数的标签,可以输入null,表示无需显示</param>
/// <param name="lblPageIndex">显示当前页码的标签,可以输入null,表示无需显示</param>
public void InitPagerControls(DataGrid mDataGrid,Label lblRowsCount,Label lblPageCount,Label lblPageIndex)
{
mDataGrid.AllowPaging = false;
this.TheGrid = mDataGrid;
this.LabelRowsCount = lblRowsCount;
this.LabelPageCount = lblPageCount;
this.LabelPageIndex = lblPageIndex;
}
/// <summary>
/// 方法编号:03
/// 内容摘要:改变查询条件重新查询数据。
/// </summary>
/// <param name="selectSQL">查询的SQL语句,翻页的时候始终使用该语句获得数据集,直到下一次调用本方法</param>
public void LoadData(string selectSQL)
{
//保存新的SQL语句
this.SelectSQL = selectSQL;
//统计符合条件的数据的条数
int mRowCount = 0;
string tmpSQL = "SELECT COUNT(*) FROM (" + selectSQL + ") t";
Database db = new Database();
System.Data.OleDb.OleDbDataReader tmpDr = db.GetDataReaderFromSQL(tmpSQL);
if(tmpDr.Read())
mRowCount = Convert.ToInt32(tmpDr[0]);
//释放数据连接
tmpDr.Close();
db.Dispose();
//如果需要则显示数据总行数
if(this.LabelRowsCount!=null)
this.LabelRowsCount.Text = mRowCount.ToString();
//统计总页数,如果需要则显示总页数
this.PageCount = (int)Math.Ceiling(((double)mRowCount)/((double)PageSizes));
if(this.LabelPageCount != null)
this.LabelPageCount.Text = this.PageCount.ToString();
//DataGrid的当前页回到第一页,按钮也返回第一页的状态
this.CurrentPageIndex = 0;
this.ChangePageButtons(0);
//查询第一页数据
this.lbtnPages_Click(this.PageButtons[0],null);
}
/// <summary>
/// 方法编号:04
/// 内容摘要:查询符合条件的数据并绑定DataGrid显示。
/// </summary>
private void BindData()
{
string selectSQL = this.SelectSQL;
if(selectSQL != string.Empty)
{
Database db = new Database();//这是我自己的一个数据操作类
int rowStart = (CurrentPageIndex * PageSizes) + 1;//当前页的起始序号
int rowEnd = (CurrentPageIndex+1) * PageSizes;//当前页的结束序号
string querySQL = "SELECT * FROM (SELECT t1.*,rownum sn FROM (" + selectSQL + ") t1) t2 " +
" WHERE t2.sn BETWEEN "+rowStart.ToString()+" AND " + rowEnd.ToString();
//查询数据库读取符合条件的数据
DataTable tb = db.GetDataTableFromSQL(querySQL);
//绑定DataGrid显示
TheGrid.DataSource = tb;
TheGrid.DataBind();
db.Dispose();
}
}
/// <summary>
/// 方法编号:05
/// 内容摘要:改变翻页按钮的状态,如显示 21、22 、 ... 30 的页码。
/// </summary>
private void ChangePageButtons(int mIndex)
{
int tmpRem = 0;
int tmpDiv = Math.DivRem(this.PageCount,10,out tmpRem);
if(tmpRem == 0) tmpRem=10;
//所有按钮恢复默认的状态
for(int i = 0;i<10;i++)
{
int pageNum = mIndex*10 + i + 1;
this.PageButtons[i].Text = pageNum.ToString();
this.PageButtons[i].Visible = true;
this.PageButtons[i].Enabled = true;
this.PageButtons[i].Font.Size = System.Web.UI.WebControls.FontUnit.XSmall;
}//end of for(int i = 0;i<10;i++)
this.lbtnPre.Visible = true;
this.lbtnNext.Visible = true;
//如果目前是1~10页,那么不显示1前面的“...”
if(mIndex == 0)
lbtnPre.Visible = false;
//如果目前是最后的一页,则不显示后面的“...”;并且保证显示的翻页按钮不超过最大页数
if(mIndex == tmpDiv)
{
lbtnNext.Visible = false;
for(int i = 9;i>tmpRem-1;i=i-1)
this.PageButtons[i].Visible = false;
}
}
/// <summary>
/// 方法编号:06
/// 内容摘要:显示前十个页码。
/// </summary>
private void lbtnPre_Click(object sender, System.EventArgs e)
{
this.ChangePageButtons((int)Math.Floor(Math.Max(this.CurrentPageIndex - 10,0)/10));
this.lbtnPages_Click(this.PageButtons[9],null);
}
/// <summary>
/// 方法编号:07
/// 内容摘要:显示后十个页码。
/// </summary>
private void lbtnNext_Click(object sender, System.EventArgs e)
{
this.ChangePageButtons((int)Math.Floor(Math.Max(this.CurrentPageIndex + 10,0)/10));
this.lbtnPages_Click(this.PageButtons[0],null);
}
/// <summary>
/// 方法编号:08
/// 内容摘要:查询绑定新的一页。
/// </summary>
private void lbtnPages_Click(object sender, System.EventArgs e)
{
LinkButton tmpLBtn = sender as LinkButton;
int pageIndex = Convert.ToInt32(tmpLBtn.Text.Trim()) - 1;
this.CurrentPageIndex = pageIndex;
foreach(LinkButton mLBtn in this.PageButtons)
{
mLBtn.Enabled = true;
mLBtn.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall;
}
tmpLBtn.Enabled = false;
tmpLBtn.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
this.BindData();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lbtnPre.Click += new System.EventHandler(this.lbtnPre_Click);
this.lbtnPage1.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage2.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage3.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage4.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage5.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage6.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage7.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage8.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage9.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnPage10.Click += new System.EventHandler(this.lbtnPages_Click);
this.lbtnNext.Click += new System.EventHandler(this.lbtnNext_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#endregion
本文地址:http://com.8s8s.com/it/it43021.htm