欢迎来到站长教程网!
兔子CDN

ASP.NET

当前位置:主页 > 网络编程 > ASP.NET >

一个支持普通分页和综合分页的MVC分页Helper

时间:2020-09-17|栏目:ASP.NET|点击:

本人写的一个分页Helper,支持普通分页(也就是,首页、上一页、下一页、末页等),综合分页(普通分页和数字分页的综合)。

下面是分页效果:

一个支持普通分页和综合分页的MVC分页Helper

分页代码:

PagerHelper.cs

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Objects.DataClasses;
namespace System.Web.Mvc
{
 public static class PagerHelper
 {
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="htmlAttributes">分页头标签属性</param>
 /// <param name="className">分页样式</param>
 /// <param name="mode">分页模式</param>
 /// <returns></returns>
 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className,PageMode mode)
 {
  TagBuilder builder = new TagBuilder("table");
  builder.IdAttributeDotReplacement = "_";
  builder.GenerateId(id);
  builder.AddCssClass(className);
  builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
  builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount,mode);
  return builder.ToString();
 }
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="className">分页样式</param>
 /// <returns></returns>
 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
 {
  return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal);
 }
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <returns></returns>
 public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount)
 {
  return Pager(helper, id, currentPageIndex, pageSize, recordCount,null);
 }
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="mode">分页模式</param>
 /// <returns></returns>
 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode)
 {
  return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,mode);
 }
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="className">分页样式</param>
 /// <param name="mode">分页模式</param>
 /// <returns></returns>
 public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode)
 {
  return Pager(helper, id, currentPageIndex, pageSize, recordCount, null,className,mode);
 }
 /// <summary>
 /// 获取普通分页
 /// </summary>
 /// <param name="currentPageIndex"></param>
 /// <param name="pageSize"></param>
 /// <param name="recordCount"></param>
 /// <returns></returns>
 private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount,PageMode mode)
 {
  int pageCount = (recordCount%pageSize ==0?recordCount/pageSize:recordCount/pageSize+1);
  StringBuilder url = new StringBuilder();
  url.Append(HttpContext.Current.Request.Url.AbsolutePath+"?page={0}");
  NameValueCollection collection = HttpContext.Current.Request.QueryString;
  string[] keys = collection.AllKeys;
  for (int i = 0; i < keys.Length; i++)
  {
  if (keys[i].ToLower() != "page")
   url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
  }
  StringBuilder sb = new StringBuilder();
  sb.Append("<tr><td>");
  sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页&nbsp;&nbsp;", recordCount, pageCount, currentPageIndex);
  if (currentPageIndex == 1)
  sb.Append("<span>首页</span>&nbsp;");
  else
  {
  string url1 = string.Format(url.ToString(), 1);
  sb.AppendFormat("<span><a href={0}>首页</a></span>&nbsp;", url1);
  }
  if (currentPageIndex > 1)
  {
  string url1 = string.Format(url.ToString(), currentPageIndex - 1);
  sb.AppendFormat("<span><a href={0}>上一页</a></span>&nbsp;", url1);
  }
  else
  sb.Append("<span>上一页</span>&nbsp;");
  if(mode == PageMode.Numeric)
  sb.Append(GetNumericPage(currentPageIndex,pageSize,recordCount,pageCount,url.ToString()));
  if (currentPageIndex < pageCount)
  {
  string url1 = string.Format(url.ToString(), currentPageIndex+1);
  sb.AppendFormat("<span><a href={0}>下一页</a></span>&nbsp;", url1);
  }
  else
  sb.Append("<span>下一页</span>&nbsp;");

  if (currentPageIndex == pageCount)
  sb.Append("<span>末页</span>&nbsp;");
  else
  {
  string url1 = string.Format(url.ToString(), pageCount);
  sb.AppendFormat("<span><a href={0}>末页</a></span>&nbsp;", url1);
  }
  return sb.ToString();
 }
 /// <summary>
 /// 获取数字分页
 /// </summary>
 /// <param name="currentPageIndex"></param>
 /// <param name="pageSize"></param>
 /// <param name="recordCount"></param>
 /// <param name="pageCount"></param>
 /// <param name="url"></param>
 /// <returns></returns>
 private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount,string url)
 {
  int k = currentPageIndex / 10;
  int m = currentPageIndex % 10;
  StringBuilder sb = new StringBuilder();
  if (currentPageIndex / 10 == pageCount / 10)
  {
  if (m == 0)
  {
   k--;
   m = 10;
  }
  else
   m = pageCount%10;
  }
  else
  m = 10;
  for (int i = k * 10 + 1; i <= k * 10 + m; i++)
  {
  if (i == currentPageIndex)
   sb.AppendFormat("<span><font color=red><b>{0}</b></font></span>&nbsp;", i);
  else
  {
   string url1 = string.Format(url.ToString(), i);
   sb.AppendFormat("<span><a href={0}>{1}</a></span>&nbsp;",url1, i);
  }
  }
  
  return sb.ToString();
 }
 }
 /// <summary>
 /// 分页模式
 /// </summary>
 public enum PageMode
 {
 /// <summary>
 /// 普通分页模式
 /// </summary>
 Normal,
 /// <summary>
 /// 普通分页加数字分页
 /// </summary>
 Numeric
 }
} 

上一篇:ASP.NET MVC4 Razor模板简易分页效果

栏    目:ASP.NET

下一篇:asp.net MVC分页代码分享

本文标题:一个支持普通分页和综合分页的MVC分页Helper

本文地址:www.dtcnnet.com/ASP_NET/31931.html

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:584415406 | 邮箱:584415406#qq.com(#换成@)

Copyright © 2015-2020 小白站长网 版权所有 苏ICP备20040415号