本人写的一个分页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""); 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}页 ", recordCount, pageCount, currentPageIndex); if (currentPageIndex == 1) sb.Append("<span>首页</span> "); else { string url1 = string.Format(url.ToString(), 1); sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1); } if (currentPageIndex > 1) { string url1 = string.Format(url.ToString(), currentPageIndex - 1); sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1); } else sb.Append("<span>上一页</span> "); 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> ", url1); } else sb.Append("<span>下一页</span> "); if (currentPageIndex == pageCount) sb.Append("<span>末页</span> "); else { string url1 = string.Format(url.ToString(), pageCount); sb.AppendFormat("<span><a href={0}>末页</a></span> ", 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> ", i); else { string url1 = string.Format(url.ToString(), i); sb.AppendFormat("<span><a href={0}>{1}</a></span> ",url1, i); } } return sb.ToString(); } } /// <summary> /// 分页模式 /// </summary> public enum PageMode { /// <summary> /// 普通分页模式 /// </summary> Normal, /// <summary> /// 普通分页加数字分页 /// </summary> Numeric } }
PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace System.Web.Mvc { public class PagerQuery<TPager,TEntityList> { public PagerQuery(TPager pager, TEntityList entityList) { this.Pager = pager; this.EntityList = entityList; } public TPager Pager { get; set; } public TEntityList EntityList { get; set; } } }
PageInfo.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace System.Web.Mvc { public class PagerInfo { public int RecordCount { get; set; } public int CurrentPageIndex { get; set; } public int PageSize { get; set; } } }
使用示例:
@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> NewsList </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>NewsList</h2> <table> <tr> <th></th> <th> NoteID </th> <th> Title </th> <th> Author </th> <th> Hit </th> <th> ReplyNum </th> </tr> <% foreach (var item in Model.EntityList) { %> <tr> <td> <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> | <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%> </td> <td> <%= Html.Encode(item.NoteID) %> </td> <td> <%= Html.Encode(item.Title) %> </td> <td> <%= Html.Encode(item.Author)%> </td> <td> <%= Html.Encode(item.Hit)%> </td> <td> <%= Html.Encode(item.ReplyNum)%> </td> </tr> <% } %> </table> <p> <%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %> </p> </asp:Content>
controler:
[AcceptVerbs(HttpVerbs.Get)] public ActionResult NewsList(int boardID,int"http://xiazai.jb51.net/201609/yuanma/MvcPager(jb51.net).rar">http://xiazai.jb51.net/201609/yuanma/MvcPager(jb51.net).rar以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月30日
2024年11月30日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]