写的不算好仅供参考
GridPagerascx:
<%@ Control Language=c# AutoEventWireup=false Codebehind=GridPagerascxcs Inherits=TestBaseClassGridPager TargetSchema=%><?xml:namespace prefix = asp /><asp:linkbutton id=lbtnPre runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False Enabled=False FontSize=Small></asp:linkbutton> <asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnPage runat=server Text= Visible=False></asp:linkbutton><asp:linkbutton id=lbtnNext runat=server Text= Visible=False></asp:linkbutton>
///////////////////////////////////////////////////////////////////////////////////////
GridPagerascxcs:
using System;
using SystemData;
using SystemDrawing;
using SystemWeb;
using SystemWebUIWebControls;
namespace TestBaseClass
{
public class GridPager : SystemWebUIUserControl
{
#region 类变量
protected SystemWebUIWebControlsLinkButton lbtnNext;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPage;
protected SystemWebUIWebControlsLinkButton lbtnPre;
#endregion
#region
/// <summary>
/// 编号
/// 内容摘要翻页的对象DataGrid控件
/// </summary>
public DataGrid TheGrid;
/// <summary>
/// 编号
/// 内容摘要显示查询结果记录数的标签可以不设置表示无需显示
/// </summary>
public Label LabelRowsCount;
/// <summary>
/// 编号
/// 内容摘要显示查询结果总页数的标签可以不设置表示无需显示
/// </summary>
public Label LabelPageCount;
/// <summary>
/// 编号
/// 内容摘要显示当前页码的标签可以不设置表示无需显示
/// </summary>
public Label LabelPageIndex;
/// <summary>
/// 编号
/// 内容摘要查询数据的SQL语句
/// </summary>
private string SelectSQL
{
get
{
if(ViewState[thisClientID+_SelectSQL] != null)
return ViewState[thisClientID+_SelectSQL]ToString();
else
return stringEmpty;
}
set
{
ViewState[thisClientID+_SelectSQL] = value;
}
}//SelectSQL结束
/// <summary>
/// 编号
/// 内容摘要每页显示的行数
/// </summary>
public int PageSizes
{
get
{
if(ViewState[thisClientID+_PageSizes] != null)
return (int)ViewState[thisClientID+_PageSizes];
else
return ;
}
set
{
ViewState[thisClientID+_PageSizes] = value;
}
}//PageSizes结束
/// <summary>
/// 编号
/// 内容摘要总页数
/// </summary>
private int PageCount
{
get
{
if(ViewState[thisClientID+_PageCount] != null)
return (int)ViewState[thisClientID+_PageCount];
else
return ;
}
set
{
ViewState[thisClientID+_PageCount] = value;
}
}//PageCount结束
/// <summary>
/// 编号
/// 内容摘要当前页码
/// </summary>
private int CurrentPageIndex
{
get
{
if(ViewState[thisClientID+_CurrentPageIndex] != null)
return (int)ViewState[thisClientID+_CurrentPageIndex];
else
return ;
}
set
{
ViewState[thisClientID+_CurrentPageIndex] = value;
if(thisLabelPageIndex!= null)
thisLabelPageIndexText = (value+)ToString();
}
}//CurrentPageIndex结束
private LinkButton[] PageButtons
{
get
{
return new LinkButton[]{lbtnPagelbtnPagelbtnPagelbtnPagelbtnPage
lbtnPagelbtnPagelbtnPagelbtnPagelbtnPage};
}
}
#endregion
#region 方法区开始
/// <summary>
/// 方法编号
/// 内容摘要初始化页面
/// </summary>
private void Page_Load(object sender SystemEventArgs e)
{
}
/// <summary>
/// 方法编号
/// 内容摘要设置与本控件关联的控件
/// </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 mDataGridLabel lblRowsCountLabel lblPageCountLabel lblPageIndex)
{
mDataGridAllowPaging = false;
thisTheGrid = mDataGrid;
thisLabelRowsCount = lblRowsCount;
thisLabelPageCount = lblPageCount;
thisLabelPageIndex = lblPageIndex;
}
/// <summary>
/// 方法编号
/// 内容摘要改变查询条件重新查询数据
/// </summary>
/// <param name=selectSQL>查询的SQL语句翻页的时候始终使用该语句获得数据集直到下一次调用本方法</param>
public void LoadData(string selectSQL)
{
//保存新的SQL语句
thisSelectSQL = selectSQL;
//统计符合条件的数据的条数
int mRowCount = ;
string tmpSQL = SELECT COUNT(*) FROM ( + selectSQL + ) t;
Database db = new Database();
SystemDataOleDbOleDbDataReader tmpDr = dbGetDataReaderFromSQL(tmpSQL);
if(tmpDrRead())
mRowCount = ConvertToInt(tmpDr[]);
//释放数据连接
tmpDrClose();
dbDispose();
//如果需要则显示数据总行数
if(thisLabelRowsCount!=null)
thisLabelRowsCountText = mRowCountToString();
//统计总页数如果需要则显示总页数
thisPageCount = (int)MathCeiling(((double)mRowCount)/((double)PageSizes));
if(thisLabelPageCount != null)
thisLabelPageCountText = thisPageCountToString();
//DataGrid的当前页回到第一页按钮也返回第一页的状态
thisCurrentPageIndex = ;
thisChangePageButtons();
//查询第一页数据
thislbtnPages_Click(thisPageButtons[]null);
}
/// <summary>
/// 方法编号
/// 内容摘要查询符合条件的数据并绑定DataGrid显示
/// </summary>
private void BindData()
{
string selectSQL = thisSelectSQL;
if(selectSQL != stringEmpty)
{
Database db = new Database();//这是我自己的一个数据操作类
int rowStart = (CurrentPageIndex * PageSizes) + ;//当前页的起始序号
int rowEnd = (CurrentPageIndex+) * PageSizes;//当前页的结束序号
string querySQL = SELECT * FROM (SELECT t*rownum sn FROM ( + selectSQL + ) t) t +
WHERE tsn BETWEEN +rowStartToString()+ AND + rowEndToString();
//查询数据库读取符合条件的数据
DataTable tb = dbGetDataTableFromSQL(querySQL);
//绑定DataGrid显示
TheGridDataSource = tb;
TheGridDataBind();
dbDispose();
}
}
/// <summary>
/// 方法编号
/// 内容摘要改变翻页按钮的状态如显示 的页码
/// </summary>
private void ChangePageButtons(int mIndex)
{
int tmpRem = ;
int tmpDiv = MathDivRem(thisPageCountout tmpRem);
if(tmpRem == ) tmpRem=;
//所有按钮恢复默认的状态
for(int i = ;i<;i++)
{
int pageNum = mIndex* + i + ;
thisPageButtons[i]Text = pageNumToString();
thisPageButtons[i]Visible = true;
thisPageButtons[i]Enabled = true;
thisPageButtons[i]FontSize = SystemWebUIWebControlsFontUnitXSmall;
}//end of for(int i = ;i<;i++)
thislbtnPreVisible = true;
thislbtnNextVisible = true;
//如果目前是~页那么不显示前面的
if(mIndex == )
lbtnPreVisible = false;
//如果目前是最后的一页则不显示后面的并且保证显示的翻页按钮不超过最大页数
if(mIndex == tmpDiv)
{
lbtnNextVisible = false;
for(int i = ;i>tmpRem;i=i)
thisPageButtons[i]Visible = false;
}
}
/// <summary>
/// 方法编号
/// 内容摘要显示前十个页码
/// </summary>
private void lbtnPre_Click(object sender SystemEventArgs e)
{
thisChangePageButtons((int)MathFloor(MathMax(thisCurrentPageIndex )/));
thislbtnPages_Click(thisPageButtons[]null);
}
/// <summary>
/// 方法编号
/// 内容摘要显示后十个页码
/// </summary>
private void lbtnNext_Click(object sender SystemEventArgs e)
{
thisChangePageButtons((int)MathFloor(MathMax(thisCurrentPageIndex + )/));
thislbtnPages_Click(thisPageButtons[]null);
}
/// <summary>
/// 方法编号
/// 内容摘要查询绑定新的一页
/// </summary>
private void lbtnPages_Click(object sender SystemEventArgs e)
{
LinkButton tmpLBtn = sender as LinkButton;
int pageIndex = ConvertToInt(tmpLBtnTextTrim()) ;
thisCurrentPageIndex = pageIndex;
foreach(LinkButton mLBtn in thisPageButtons)
{
mLBtnEnabled = true;
mLBtnFontSize = SystemWebUIWebControlsFontUnitXSmall;
}
tmpLBtnEnabled = false;
tmpLBtnFontSize = SystemWebUIWebControlsFontUnitSmall;
thisBindData();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASPNET Web 窗体设计器所必需的
//
InitializeComponent();
baseOnInit(e);
}
/// <summary>
///设计器支持所需的方法 不要使用代码编辑器
///修改此方法的内容
/// </summary>
private void InitializeComponent()
{
thislbtnPreClick += new SystemEventHandler(thislbtnPre_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnPageClick += new SystemEventHandler(thislbtnPages_Click);
thislbtnNextClick += new SystemEventHandler(thislbtnNext_Click);
thisLoad += new SystemEventHandler(thisPage_Load);
}
#endregion
#endregion