在ASP.NET开发中,常用到显示分页页码程序,以下是本人写的一个类,还未完善,但已可使用,
在显示时当前页码会自动据中。并可自定义分类链接代码
using System;
namespace bookshopcn.Service
{
/// <summary>
/// Page 的摘要说明。
/// </summary>
public class Pager
{
public Pager(){}
protected static int _ButtonCount = 11;
protected static string _NextPage = "<a href={0}>下一页</a>";
protected static string _LinkUrl = "?page={0}";
protected static string _LastPage = "<a href={0}>上一页</a>";
/// <summary>
/// 下一页链接
/// </summary>
public static string NextPage
{
get{return _NextPage;}
set{_NextPage = value;}
}
/// <summary>
/// 上一页链接
/// </summary>
public static string LastPage
{
get{return _LastPage;}
set{_LastPage = value;}
}
/// <summary>
/// 设置时为格式
/// </summary>
public static string NextPageText
{
get{return _NextPage;}
set{_NextPage = value;}
}
/// <summary>
/// 显示按钮总数
/// </summary>
public static int BottonCount
{
get{return _ButtonCount;}
set{_ButtonCount = value;}
}
/// <summary>
/// 返回页面的分页信息
/// </summary>
/// <param name="_RecordCount">记录总数</param>
/// <param name="_PageSize">分页长度</param>
/// <param name="_PageIndex">当前页</param>
/// <returns></returns>
public static string PageInfo(int _RecordCount,int _PageSize,int _PageIndex,string link)
{
string Firstpage = string.Format("<a href="+link+">[首页]</a>","1");
string pageinfo = "共有{0}页 / 当前第{1}页 "+Firstpage;
string pagenext = " <a href="+link+"><b>{0}</b></a> ";
int PageCount = _RecordCount / _PageSize; // 页数合计
PageCount = PageCount <= 0?1:PageCount;
pageinfo = string.Format(pageinfo,PageCount.ToString(),_PageIndex.ToString());
string LastPage = string.Format("<a href="+link+">[末页]</a>",PageCount);
int n = _ButtonCount/2; //左右显示个数
int StartPage = _PageIndex - n;
int EndPage = _PageIndex + n;
_LastPage = string.Format(_LastPage,link); //上一页
_LastPage = _PageIndex-1>1?string.Format(_LastPage,(_PageIndex-1).ToString()):string.Format(_NextPage,"1");
_NextPage = string.Format(_NextPage,link); //下一页
_NextPage = _PageIndex+1<=PageCount?string.Format(_NextPage,_PageIndex.ToString()):string.Format(_NextPage,PageCount.ToString());
if(EndPage > PageCount )
{
StartPage = (_PageIndex - n) - (EndPage-PageCount);
EndPage = PageCount ;
}
if(StartPage < 1 )
{
EndPage = _ButtonCount;
StartPage = 1 ;
}
for(int i = StartPage;i<=EndPage;i++)
{
if(i != _PageIndex)
pageinfo += string.Format(pagenext,i);
else
pageinfo += " <b>" + i.ToString() + "</b> ";
}
pageinfo += LastPage;
return pageinfo;
}
}
}