在顯示時當前頁碼會自動據中。並可自訂分類連結代碼
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;
}
}
}