/// <summary>
/// 數據分頁方法
/// </summary>
/// <param name="PageIndex">當前頁</param>
/// <param name="PageSize">每頁顯示數量</param>
/// <param name="PageCount">總數據</param>
/// <param name="Url">鏈接,如:list.aspx?id=1234</param>
/// <returns></returns>
public static string GetPage(int PageIndex, int PageSize, int RecordCount, string Url)
{
StringBuilder sb = new StringBuilder();
try
{
//計算總頁數
int PageCount = RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
if (PageIndex < 1)
{
PageIndex = 1;
}
if (PageIndex > PageCount)
{
PageIndex = PageCount;
}
string StarPage = "";//首頁
string EndPage = "";//尾頁
string PrePage = "";//上一頁
string NextPage = "";//下一頁
//首頁和上一頁的鏈接
if (PageIndex <= 1 || PageCount <= 1)
{
StarPage = "";
PrePage = "";
}
else
{
StarPage = "";
PrePage = "<li class=\"previous\"><a href=\"" + Url + "&page=" + (PageIndex - 1) + "\">上一頁</a></li>";
}
//末頁和下一頁的鏈接
if (PageIndex == PageCount || PageCount <= 1)
{
EndPage = "";
NextPage = "";
}
else
{
EndPage = "";
NextPage = "<li class=\"next\"><a href=\"" + Url + "&page=" + (PageIndex + 1) + "\">下一頁</a></li>";
}
//頁碼輸出
int PagerStart = 1;//第一個頁碼
if (PageCount >= 5)
{
PagerStart = PageIndex % 5 == 0 ? PageIndex - 2 : PageIndex - PageIndex % 5;
}
if (PagerStart < 1)
{
PagerStart = 1;
}
string NumBtn = "";
for (int i = PagerStart; i < PagerStart + 5 && i <= PageCount; i++)
{
if (i == PageIndex)
{
NumBtn += "<li class=\"current\"><a>" + i + "</a>";
}
else
{
NumBtn += "<li><a href=\"" + Url + "&page=" + i + "\">" + i + "</a></li>";
}
}
sb.Append(StarPage + PrePage + NumBtn + NextPage + EndPage);
}
catch
{
sb.Append("");
}
return sb.ToString();
}