写的不算好,仅供参考。
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.PageBut