适用于WebForm Mvc的Pager分页组件C#实现
程序员文章站
2022-05-25 23:09:08
本文为大家分享了自己写的一个pager分页组件,webform,mvc都适用,具体内容如下
分页控件其实就是根据链接在页面间传递参数,因为我看到mvc中你可以看到这样传递...
本文为大家分享了自己写的一个pager分页组件,webform,mvc都适用,具体内容如下
分页控件其实就是根据链接在页面间传递参数,因为我看到mvc中你可以看到这样传递参数的new {para=val}这种方式传递参数,于是我想到用可以模仿这种传递参数的方式,那就用dynamic来作为参数对象传递。
下面是附上我写的具体的实现的代码
数据处理代码:
1.定义ipagedlist接口
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace infrastruction.pager { public interface ipagedlist { int pageindex { get; set; } int pagesize { get; set; } int totalitemcount { get; set; } int totalpagecount { get; } } }
2.实现ipagedlist接口
using system; using system.collections.generic; using system.linq; using system.web; namespace infrastruction.pager { public class pagedlist<t> : list<t>, ipagedlist { public int pageindex { get; set; } public int pagesize { get; set; } public int totalitemcount { get; set; } public int totalpagecount { get { return totalitemcount % pagesize == 0 ? (totalitemcount / pagesize) : (totalitemcount / pagesize + 1); } } public pagedlist(ienumerable<t> sources, int pageindex, int pagesize) { if (sources != null && sources.any()) { this.addrange(sources.skip(pagesize * (pageindex - 1)).take(pagesize).tolist()); } this.pageindex = pageindex; this.pagesize = pagesize; this.totalitemcount = sources.count(); } } }
分页标签处理代码:
3.pagerhelper
using infrastruction.pager; using system; using system.collections.generic; using system.linq; using system.reflection; using system.text; using system.web; using system.web.ui; namespace system.web.ui { public static class pagerhelper { public static string pager(string url, ipagedlist pagedlist) { stringbuilder builder = new stringbuilder(); if (pagedlist != null) { builder.append("<script type='text/javascript'>"); builder.append("window.onload = function () {"); builder.append(" var elements = document.getelementbyid('pager').childnodes;"); builder.append(" for (var i = 0; i < elements.length; i++) {"); builder.append("var txt = elements[i].innertext || elements[i].textcontent;"); builder.append(" if (elements[i].nodetype =='1' && txt == '" + pagedlist.pageindex + "') {"); builder.append("elements[i].style.textdecoration = 'underline';break; } } }"); builder.append("</script>"); builder.append("<div id='pager'>"); builder.append("<span class='p'>"); builder.appendformat("共 {0} 条数据 页次:{1}/{2}", pagedlist.totalitemcount, pagedlist.pageindex, pagedlist.totalpagecount, "上一页"); builder.append("</span>"); builder.append(" "); builder.append(" "); builder.append(" "); builder.append(" "); if (pagedlist.pageindex > 1 && pagedlist.pageindex <= pagedlist.totalpagecount) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, 1, "首页"); builder.append("</span>"); builder.append(" "); builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, pagedlist.pageindex - 1, "上一页"); builder.append("</span>"); builder.append(" "); } if (pagedlist.totalpagecount > 1 && pagedlist.totalpagecount <= 10) { for (int i = 1; i <= pagedlist.totalpagecount; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, i, i); builder.append("</span>"); builder.append(" "); } } else if (pagedlist.totalpagecount > 10) { if (pagedlist.pageindex < 11) { for (int i = 1; i <= 10; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, i, i); builder.append("</span>"); builder.append(" "); } builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, 11, "..."); builder.append("</span>"); builder.append(" "); } else { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, (pagedlist.pageindex - 6), "..."); builder.append("</span>"); builder.append(" "); if (pagedlist.pageindex >= 11 && pagedlist.totalpagecount <= pagedlist.pageindex + 5) { for (int i = pagedlist.pageindex - 5; i <= pagedlist.totalpagecount; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, i, i); builder.append("</span>"); builder.append(" "); } } else { for (int i = pagedlist.pageindex - 5; i <= pagedlist.pageindex + 5; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, i, i); builder.append("</span>"); builder.append(" "); } builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, (pagedlist.pageindex + 6), "..."); builder.append("</span>"); builder.append(" "); } } } if (pagedlist.pageindex >= 1 && pagedlist.pageindex < pagedlist.totalpagecount) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, pagedlist.pageindex + 1, "下一页"); builder.append("</span>"); builder.append(" "); builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?pageindex={1}'>{2}</a>", url, pagedlist.totalpagecount, "尾页"); builder.append("</span>"); builder.append(" "); } builder.append("</div>"); } return builder.tostring(); } public static string pager(string url, ipagedlist pagedlist, dynamic objattr) { stringbuilder builder = new stringbuilder(); if (pagedlist != null) { builder.append("<script type='text/javascript'>"); builder.append("window.onload = function () {"); builder.append(" var elements = document.getelementbyid('pager').childnodes;"); builder.append(" for (var i = 0; i < elements.length; i++) {"); builder.append("var txt = elements[i].innertext || elements[i].textcontent;"); builder.append(" if (elements[i].nodetype =='1' && txt == '" + pagedlist.pageindex + "') {"); builder.append("elements[i].style.textdecoration = 'underline';break; } } }"); builder.append("</script>"); string paras = ""; propertyinfo[] infos = objattr.gettype().getproperties(); if (infos != null && infos.any()) { foreach (var item in infos) { paras += string.format("{0}={1}", item.name, item.getvalue(objattr, null)); paras += "&"; } } paras = paras + "pageindex="; builder.append("<div id='pager'>"); builder.append("<span class='p'>"); builder.appendformat("共 {0} 条数据 页次:{1}/{2}", pagedlist.totalitemcount, pagedlist.pageindex, pagedlist.totalpagecount, "上一页"); builder.append("</span>"); builder.append(" "); builder.append(" "); builder.append(" "); builder.append(" "); if (pagedlist.pageindex > 1 && pagedlist.pageindex <= pagedlist.totalpagecount) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + 1, "首页"); builder.append("</span>"); builder.append(" "); builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + (pagedlist.pageindex - 1), "上一页"); builder.append("</span>"); builder.append(" "); } if (pagedlist.totalpagecount > 1 && pagedlist.totalpagecount <= 10) { for (int i = 1; i <= pagedlist.totalpagecount; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + i, i); builder.append("</span>"); builder.append(" "); } } else if (pagedlist.totalpagecount > 10) { if (pagedlist.pageindex < 11) { for (int i = 1; i <= 10; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + i, i); builder.append("</span>"); builder.append(" "); } builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + 11, "..."); builder.append("</span>"); builder.append(" "); } else { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + (pagedlist.pageindex - 6), "..."); builder.append("</span>"); builder.append(" "); if (pagedlist.pageindex >= 11 && pagedlist.totalpagecount <= pagedlist.pageindex + 5) { for (int i = pagedlist.pageindex - 5; i <= pagedlist.totalpagecount; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + i, i); builder.append("</span>"); builder.append(" "); } } else { for (int i = pagedlist.pageindex - 5; i <= pagedlist.pageindex + 5; i++) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + i, i); builder.append("</span>"); builder.append(" "); } builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + (pagedlist.pageindex + 6), "..."); builder.append("</span>"); builder.append(" "); } } } if (pagedlist.pageindex >= 1 && pagedlist.pageindex < pagedlist.totalpagecount) { builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + (pagedlist.pageindex + 1), "下一页"); builder.append("</span>"); builder.append(" "); builder.append("<span class='p'>"); builder.appendformat("<a href='{0}?{1}'>{2}</a>", url, paras + pagedlist.totalpagecount, "尾页"); builder.append("</span>"); builder.append(" "); } builder.append("</div>"); } return builder.tostring(); } } }
4. pagerlinqextension( 基于linq的扩展)
using system; using system.collections.generic; using system.linq; using system.linq.expressions; using system.web; namespace infrastruction.pager { public static class pagerlinqextension { public static pagedlist<t> topagedlist<t>(this iqueryable<t> source, int pageindex, int pagesize) { return new pagedlist<t>(source, pageindex, pagesize); } } }
调用方法
1.webform调用: <%=pagerhelper.pager("products.aspx", pagelist, new { cid=cid})%> 或者用literal在后台绑定也行
2.mvc调用:
需要扩展一下方法
namespace system.web.mvc.html { public static class htmlextension { public static ihtmlstring pager(this htmlhelper helper, string url, ipagedlist pagedlist) { return helper.raw(pagerhelper.pager(url, pagedlist)); } public static ihtmlstring pager(this htmlhelper helper, string url, ipagedlist pagedlist, dynamic objattr) { return helper.raw(pagerhelper.pager(url, pagedlist, objattr)); } } }
然后页面调用@html.pager("products.aspx", pagelist, new { cid=cid,......})
全部的代码都在上面,希望大家认真学习,对大家学习使用分页控件有所帮助。