一个支持普通分页和综合分页的MVC分页Helper
程序员文章站
2023-08-17 15:03:38
本人写的一个分页helper,支持普通分页(也就是,首页、上一页、下一页、末页等),综合分页(普通分页和数字分页的综合)。
下面是分页效果:
分页代码:...
本人写的一个分页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}页 ", 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? page) { pagerinfo pager = new pagerinfo(); newsarticleinfo info = new newsarticleinfo(); info.newsboard = new newsboardinfo(); info.newsboard.boardid = boardid; pager.recordcount = resolve<inewsbll>().getarticledatalist(info, articletypeenum.pass); pager.pagesize = 10; pager.currentpageindex = (page!=null?(int)page:1); ilist<newsarticleinfo> result = resolve<inewsbll>().getarticledatalist(pager.currentpageindex, pager.pagesize, articletypeenum.pass, info); pagerquery<pagerinfo, ilist<newsarticleinfo>> query = new pagerquery<pagerinfo, ilist<newsarticleinfo>>(pager,result); return view(query); }
源码下载:http://xiazai.jb51.net/201609/yuanma/mvcpager(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。