This article for everyone to share their own write a pager paging components, WEBFORM,MVC are applicable, specific content as follows
Paging controls actually pass parameters between pages based on links, because I see in MVC that you can see the new {para=val} passing arguments in this way, so I think of using dynamic as a Parameter object in a way that can mimic this passing parameter.
Here is the attached code for the specific implementation I wrote
Data processing code:
1. Define Ipagedlist Interface
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. Implement Ipagedlist interface
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 && so Urces. Any ())
{this
. AddRange (sources. Skip (PageSize * (pageIndex-1)). Take (pageSize). ToList ());
}
This.pageindex = PageIndex;
This.pagesize = pageSize;
This.totalitemcount = sources. Count ();}}}
Pagination label Processing code:
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 pag
edlist) {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 ("Total {0} data page times: {1}/{2}", Pagedlist.totalitemcount, Pagedlist.pageindex, Pagedlist.totalpagecount, "prev"); 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, "Home"); Builder.
Append ("</span>"); Builder.
Append (" "); Builder.
Append ("<span class= ' P ' >"); Builder.
AppendFormat ("<a href= ' {0}?pageindex={1} ' >{2}</a>", url, pagedlist.pageindex-1, "prev"); Builder.
Append ("</span>"); BuildeR.append (" "); } if (Pagedlist.totalpagecount > 1 && pagedlist.totalpagecount <=) {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 >) {if (Pagedlist.pageindex < 11) {for (int i = 1; I <= 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 >= && 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.totalpageco UNT) {Builder.
Append ("<span class= ' P ' >"); Builder.
AppendFormat ("<a href= ' {0}?pageindex={1} ' >{2}</a>", url, Pagedlist.pageindex + 1, "next page"); Builder.
Append ("</span>"); Builder. AppenD (" "); Builder.
Append ("<span class= ' P ' >"); Builder.
AppendFormat ("<a href= ' {0}?pageindex={1} ' >{2}</a>", url, Pagedlist.totalpagecount, "last"); 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 ("Total {0} data page times: {1}/{2}", Pagedlist.totalitemcount, Pagedlist.pageindex, Pagedlist.totalpagecount, "prev"); 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, "homepage"); Builder.
Append ("</span>"); Builder.
Append (" "); Builder.
Append ("<span class= ' P ' >"); Builder. AppendFormat ("<a href= ' {0}?{
1} ' >{2}</a> ', URL, paras + (pagedlist.pageindex-1), "prev"); Builder.
Append ("</span>"); Builder.
Append (" "); } if (Pagedlist.totalpagecount > 1 && pagedlist.totalpagecount <=) {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 > {if (Pagedlist.pageindex <) {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 >= && 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.pagei Ndex + 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.TOTALPAGEC Ount) {Builder.
Append ("<span class= ' P ' >"); Builder. AppendFormat ("<a href= ' {0}?{
1} ' >{2}</a> ', URL, paras + (Pagedlist.pageindex + 1), "next page"); Builder.
Append ("</span>"); Builder.
Append (" "); Builder.
Append ("<span class= ' P ' >"); Builder. AppendFormat ("<a href= ' {0}?{
1} ' >{2}</a> ', URL, paras + pagedlist.totalpagecount, "last"); Builder.
Append ("</span>"); Builder.
Append (" "); } builder.
Append ("</div>"); Return builder.
ToString ();
}
}
}
4. Pagerlinqextension (based on LINQ extensions)
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);}}
Call method
1.Webform Call: <%=pagerhelper.pager ("Products.aspx", PageList, new {cid=cid})%> or bound in the background with literal
2.MVC Call:
Need to extend the method
Namespace System.Web.Mvc.Html
{public
static class Htmlextension
{public
static ihtmlstring Pager ( This is 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)
{
ret Urn Helper. Raw (Pagerhelper.pager (URL, pagedlist, objattr));}}
The page then calls @Html. Pager ("Products.aspx", PageList, new {cid=cid,......})
All the code is on the above, I hope you study hard, for everyone to learn to use pagination control to help.