c# asp.net 实现分页(pager)功能
分页PagerHelper辅助类
using System;
using System.Web;
public class PagerHelper
{
#region 获取分页的Html代码
/// <summary>
/// 获取分页的Html代码
/// 当前页码方法内部根据Request["page"]获取
/// </summary>
/// <param>每一页数量</param>
/// <param>总数量</param>
/// <param>伪静态地址如/news/list-1-{0}.html</param>
/// <param>最多显示的页码个数(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=http://www.mamicode.com/\"javascript:void(0);\">首页</a>" : "<a href=http://www.mamicode.com/\"" + string.Format(url, 1) + "http://www.mamicode.com/\">首页</a>");
pageHtml.Append(curPageIndex > 1 ? "<a href=http://www.mamicode.com/\"" + string.Format(url, curPageIndex - 1) + "http://www.mamicode.com/\">上一页</a>" : "<a href=http://www.mamicode.com/\"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=http://www.mamicode.com/\"" + string.Format(url, firstNum - 1) + "http://www.mamicode.com/\">...</a>");
}
for (int i = firstNum; i < firstNum + maxPageNum; i++)
{
if (i > pageCount) break;
string css = string.Empty;
if (i == curPageIndex)
{
css = "class=http://www.mamicode.com/\"currentpage\"";
}
pageHtml.Append("<a " + css + " href=http://www.mamicode.com/\"" + string.Format(url, i) + "http://www.mamicode.com/\">" + i + "</a>");
}
if (pageCount >= firstNum + maxPageNum)
{
pageHtml.Append("<a href=http://www.mamicode.com/\"" + string.Format(url, firstNum + maxPageNum) + "http://www.mamicode.com/\">...</a>");
}
//if (pageCount > curPageIndex)
//{
pageHtml.Append(curPageIndex < pageCount ? "<a href=http://www.mamicode.com/\"" + string.Format(url, curPageIndex + 1) + "http://www.mamicode.com/\">下一页</a>" : "<a href=http://www.mamicode.com/\"javascript:void(0);\">下一页</a>");
pageHtml.Append("<a href=http://www.mamicode.com/\"" + string.Format(url, pageCount) + "http://www.mamicode.com/\">尾页</a>");
//}
pageHtml.Append(string.Format("<a href=http://www.mamicode.com/\"javascript:void(0);\">共{0}页,{1}条</a>", pageCount, totalCount));
return pageHtml.ToString();
}
#endregion
#region 获取页码总数
/// <summary>
/// 获取页码总数
/// </summary>
/// <param>每一页 数量</param>
/// <param>总数量</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 runat="server"> <div> <asp:Literal runat="server"></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>
预览图
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/38499.html