Pagination control under ASP. Mvcpager It's just too good to be true. Two lines of code implement a paging, and need to change the style of the direct construction of the Pageroptions class
The actual combat need not say more, take to use it can. Personal feel that the impact on the performance of things, if not feel its coding ideas, just understand the use of functions, without deep, and people's life is limited, the choice also varies
First, the simple concatenation of the string of pagination Csspager, directly modified eight to eight styles and strings can be used
Encapsulated HTML extension method:
<summary>///page///</summary>/<param name= "HtmlHelper" ></param> <param name= "pageSize" ></param>//<param name= "currentpage" ></param>// <param name= "TotalCount" ></param>//<param name= "href" ></param>//<RETURNS&G t;</returns> public static string Getpager (this htmlhelper htmlhelper, int pageSize, int currentpage, int tot Alcount, String href) {pageSize = PageSize = = 0? 3:pagesize; var totalpages = Math.max ((totalcount + pageSize-1)/pageSize, 1); Total pages var output = new StringBuilder (); if (TotalPages > 1) {if (currentpage! = 1) {//Processing home Connection OUTPU T.appendformat ("<li><a class= ' news_next3 ' href= ' {0}pageindex=1&pagesize={1} ' > Home </a></li > ", href, pageSize); } else {output. Append ("<li><a class= ' news_next3 ' style= ' cursor:default;background-color: #C4C4C4; ' href= ' javascript:void (0) ' > Home </a></li> '); } if (CurrentPage > 1) {//processes the previous page of connection output. AppendFormat ("<li><a class= ' news_next3 ' href= ' {0}pageindex={1}&pagesize={2} ' >? </a></li> ", href, currentPage-1, pageSize); } else {output. Append ("<li><a class= ' news_next3 ' style= ' cursor:default;background-color: #C4C4C4; ' href= ' javascript:void (0) ' >? </a></li> "); } output. Append (""); int start = 0, end = 0, Tmpstart = 0, tmpend = 0; if (currentpage >= 1 && currentPage-2 >= 1) {start = CurrentPage-2; } else if (CurrentpagE >= 1 && currentPage-2 < 1) {start = 1; Tmpend = 2-currentpage + 1; } if (TotalPages >= currentpage && currentpage + 2 <= totalpages) { End = CurrentPage + 2; } else if (totalpages >= currentpage && currentpage + 2 > TotalPages) { end = TotalPages; Tmpstart = (currentpage + 2)-totalpages; } start = Start-tmpstart < 1? 1:start-tmpstart; End = end + Tmpend > totalpages? Totalpages:end + tmpend; for (int i = start; I <= end; i++) {if (currentpage! = i) { Output. AppendFormat ("<li><a class= ' news_next3 ' href= ' {0}pageindex={1}&pagesize={2} ' >{3}</a></ Li> ", href, I, pAgesize, i); } else {output. AppendFormat ("<li><a class= ' news_next2 ' href= ' {0}pageindex={1}&pagesize={2} ' >{3}</a></ Li> ", href, I, pageSize, i); }} if (CurrentPage < totalpages) {//processes the next page of link output. AppendFormat ("<li><a class= ' news_next3 ' href= ' {0}pageindex={1}&pagesize={2} ' >? </a></li> ", href, currentpage + 1, pageSize); } else {output. Append ("<li><a class= ' news_next3 ' style= ' cursor:default;background-color: #C4C4C4; ' href= ' javascript:void (0) ' >? </a></li> "); } output. Append (""); if (currentpage! = totalpages) {output. AppendFormat ("<li><a class= ' news_next3 ' href= ' {0}pageindex={1}&pagesiZE={2} ' > End </a></li> ', href, totalpages, pageSize); } else {output. Append ("<li><a class= ' news_next3 ' style= ' cursor:default;background-color: #C4C4C4; ' href= ' javascript:void (0) ' > Last </a></li> '); } output. Append (""); }//output. AppendFormat ("<p> total {0} Records {1}/{2} page </p>", TotalCount, CurrentPage, totalpages); return output. ToString (); }
Call in the view, the style package is not affixed, the source has
@Html. Raw (Html.getpager (3, Model.currentpageindex, Model.totalitemcount, String. Format ("?cid={0}&nid={1}&", 1, 2)))
Second, mvcpager two lines of code: action in the new Pagedlist (version 2.0 directly call ienumerable<t> topagedlist extension method), Html.pager extension method called Mvcpager encapsulation in view
Action:
Public ActionResult Index (int pageIndex = 1, int pageSize = 3) { list<people> List = new List<people> ( { New people () {id=1,name= "Lily", age=16}, ... }; var data = list. Skip ((pageIndex-1) * pageSize). Take (pageSize); Pagedlist<people> PL = new pagedlist<people> (data, PageIndex, PageSize, list. Count); if (Request.isajaxrequest ()) { return Partialview ("_ajaxindex", pl); } return View (PL); }
1. Default display style
@Html. Pager (Model, New Pageroptions () { pageindexparametername = "PageIndex", Showpageindexbox = True, Pageindexboxtype = pageindexboxtype.dropdownlist, Showgobutton = True, pageindexboxwrapperformatstring = "Jump to page {0}" })
2, add most of the styles can be constructed pageroptions to the property assignment can be done, but to the original generated a tag to add style will need to modify the source code
@Html. Pager (Model, New Pageroptions () {//Sub-page label, default to div containertagname = "ul",//Up, down One page//"<span class=\" news_next3\ ">? </span> "HTML will be rendered Prevpagetext ="? ", NextPageText ="? ",//default//FIRSTPA Getext = "Home",//lastpagetext = "Last",//alwaysshowfirstlastpagenumber = False,//showprevnex T = true, Pageindexparametername = "PageIndex",//Show drop-down page//showpageindexbox = True, Pageindexboxtype = pageindexboxtype.dropdownlist,//showgobutton = true,//gobuttontext = "Go", pageindexboxwrapperformatstring = "Jump to page {0}",////a label for first, last, top, bottom, and page numbers, all nested spans on the outside of a label//PAGERITEMWR apperformatstring = "<span class=\" news_next3\ ">{0}</span>",//Current page number (does not include first, last, top, bottom) nested span label C urrentpageritemwrapperformatstring = "<li><span class=\" news_next2\ ">{0}</span></Li> ",//other page numbers (does not contain first, last, Upper, lower) nested span tags numericpageritemwrapperformatstring =" <li><span class=\ "News_next3\" >{0}</span></li> ",//First, last, upper, lower (without page numbers) nested span tags, opposite navigationpageritemwrapp erformatstring = "<li><span class=\" news_next3\ ">{0}</span></li>",//pageindex out of range, general direct repair Change URL and delete Last page data occurs invalidpageindexerrormessage = "page index is invalid", Pageindexoutofrangeerrormessage = "page index out of range" })
3, the image to the right of the Ajax paging, call Ajax.pager and Html.ajaxpager are all right, the only thing to note is to put the paging control inside the Updatetargetid
@Ajax. Pager (Model, New Pageroptions () { pageindexparametername = "PageIndex", Showpageindexbox = True, Pageindexboxtype = pageindexboxtype.dropdownlist, Showgobutton = True, pageindexboxwrapperformatstring = "Jump to page {0}" }, New Ajaxoptions () {Updatetargetid = "upid" })
Source: Http://files.cnblogs.com/NotAnEmpty/MvcPager.rar
Combat Mvcpager (Pageroptions custom style & Same, asynchronous)