分頁PagerHelper輔助類
using System;
using System.Web;
public class PagerHelper
{
#region 獲取分頁的Html代碼
/// <summary>
/// 獲取分頁的Html代碼
/// 當前頁碼方法內部根據Request["page"]獲取
/// </summary>
/// <param name="pageSize">每一頁數量</param>
/// <param name="totalCount">總數量</param>
/// <param name="url">偽靜態地址如/news/list-1-{0}.html</param>
/// <param name="maxPageNum">最多顯示的頁碼個數(100頁 每次只顯示8個其他隱藏)</param>
/// <returns></returns>
public static string GetPageHtml(int pageSize, int totalCount, string url, int maxPageNum = 8)
{
HttpRequest Request = HttpContext.Current.Request;
int curPageIndex = 1;
if (!string.IsNullOrWhiteSpace(Request["page"]))
{
curPageIndex = Convert.ToInt32(Request["page"] ?? "1");
curPageIndex = curPageIndex <= 0 ? 1 : curPageIndex;
}
System.Text.StringBuilder pageHtml = new System.Text.StringBuilder();
//if (pageIndex > 1)
//{
pageHtml.Append(curPageIndex == 1 ? "<a href=\"javascript:void(0);\">首頁</a>" : "<a href=\"" + string.Format(url, 1) + "\">首頁</a>");
pageHtml.Append(curPageIndex > 1 ? "<a href=\"" + string.Format(url, curPageIndex - 1) + "\">上一頁</a>" : "<a href=\"javascript:void(0);\">上一頁</a>");
//}
int pageCount = GetPageCount(pageSize, totalCount);//總頁碼
//獲取顯示區域第一個開始位置 如 1 9 17
int firstNum = curPageIndex % maxPageNum == 0 ? curPageIndex - (maxPageNum - 1) : curPageIndex - curPageIndex % maxPageNum + 1;
if (firstNum > maxPageNum)
{
pageHtml.Append("<a href=\"" + string.Format(url, firstNum - 1) + "\">...</a>");
}
for (int i = firstNum; i < firstNum + maxPageNum; i++)
{
if (i > pageCount) break;
string css = string.Empty;
if (i == curPageIndex)
{
css = "class=\"currentpage\"";
}
pageHtml.Append("<a " + css + " href=\"" + string.Format(url, i) + "\">" + i + "</a>");
}
if (pageCount >= firstNum + maxPageNum)
{
pageHtml.Append("<a href=\"" + string.Format(url, firstNum + maxPageNum) + "\">...</a>");
}
//if (pageCount > curPageIndex)
//{
pageHtml.Append(curPageIndex < pageCount ? "<a href=\"" + string.Format(url, curPageIndex + 1) + "\">下一頁</a>" : "<a href=\"javascript:void(0);\">下一頁</a>");
pageHtml.Append("<a href=\"" + string.Format(url, pageCount) + "\">尾頁</a>");
//}
pageHtml.Append(string.Format("<a href=\"javascript:void(0);\">共{0}頁,{1}條</a>", pageCount, totalCount));
return pageHtml.ToString();
}
#endregion
#region 獲取頁碼總數
/// <summary>
/// 獲取頁碼總數
/// </summary>
/// <param name="pageSize">每一頁 數量</param>
/// <param name="totalCount">總數量</param>
/// <returns></returns>
private static int GetPageCount(int pageSize, int totalCount)
{
int pageNumbers = 0;
if (totalCount % pageSize != 0)
{
pageNumbers = totalCount / pageSize + 1;
}
else
{
pageNumbers = totalCount / pageSize;
}
pageNumbers = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalCount) / Convert.ToDouble(pageSize)));
return pageNumbers;
}
#endregion
}
前台使用
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, i { margin: 0; padding: 0; border: 0; font-weight: inherit; font-style: inherit; font-size: 100%; font-family: inherit; vertical-align: baseline; }
body { background: #fff; font: 12px/1.5 Tahoma; color: #000; }
a { text-decoration: none; cursor: pointer; }
/*分頁*/
.page { clear: both; text-align: center; margin-top: 10px; margin-bottom: 20px; }
.page a { border: 1px solid #dbdbdb; background: #fff; padding: 5px 10px; margin: 1px; display: inline-block; color: #000; }
.page a:hover { text-decoration: none; background-color: #2196F3; color: #fff; }
.page span a { border: 1px solid #1f5b13; background: #fff; padding: 2px 7px; margin: 1px; display: inline-block; color: #104c00; }
.page span a:hover { text-decoration: none; background-color: #a3c79c; }
.page .currentpage { background-color: #ff8800; color: #fff; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="page">
<asp:Literal runat="server" ID="ltHtml"></asp:Literal>
</div>
</form>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
ltHtml.Text = PagerHelper.GetPageHtml(3, 33, "/tpager_demo.aspx?page={0}");
}
</script>
</body>
</html>
預覽圖

