ASP. NET MVC Custom pager encapsulation and optimization

Source: Internet
Author: User

ASP. NET MVC Custom pager encapsulation and optimization intro

Before doing a common paging component, but some of the shortcomings, from page-flipping events and page styles are fused in the background code, to page flip event can be customized, and then page and style are separated from the code, custom paging pager more and more easy to expand.

HtmlHelper Pager Extension Pager V1.0:
1     /// <summary>2     ///Pager V1.03     /// </summary>4     /// <param name= "helper" >HtmlHelper</param>5     /// <param name= "Pager" >Paging Information</param>6     /// <returns></returns>7      Public StaticMvchtmlstring Pager ( ThisHtmlHelper Helper, Pagermodel Pager)8     {9StringBuilder Sbhtmltext =NewStringBuilder ();TenSbhtmltext.append ("<div style=\ "text-align:center\" ><nav><ul class=\ "pagination\" >"); One         if(Pager. PageIndex <=1) A         { -Sbhtmltext.append ("<li class=\ "disabled\" ><a href=\ "javascript:void (0) \" aria-label=\ "previous\" ><span Aria-hidden =\ "True\" >&laquo;</span></a></li>"); -         } the         Else -         { -Sbhtmltext.appendformat ("<li><a href=\ "javascript:loaddata (1) \" aria-label=\ "1\" ><span aria-hidden=\ "true\" >& Laquo;</span></a></li>", Pager. PageIndex-1); -Sbhtmltext.appendformat ("<li><a href=\ "Javascript:loaddata ({0}) \" >{0}</a></li>", Pager. PageIndex-1); +         } -Sbhtmltext.appendformat ("<li class=\ "active\" ><a href=\ "javascript:void (0) \" >{0}<span class=\ "sr-only\" > (current) </span></a></li>", Pager. PageIndex); +         if(Pager. PageIndex >=Pager. PageCount) A         { atSbhtmltext.append ("<li class=\ "disabled\" ><a href=\ "javascript:void (0) \" aria-label=\ "next\" ><span aria-hidden=\ " True\ ">&raquo;</span></a></li>"); -         } -         Else -         { -Sbhtmltext.appendformat ("<li><a href=\ "Javascript:loaddata ({0}) \" >{0}</a></li>", Pager. PageIndex +1); -Sbhtmltext.appendformat ("<li><a href=\ "Javascript:loaddata ({0}) \" Aria-label=\ "next\" ><span aria-hidden=\ "true\" > &raquo;</span></a></li>", Pager. PageIndex +1); in         } -Sbhtmltext.append ("</ul></nav>"); toSbhtmltext.appendformat ("<div><span> Each page has <strong>{0}</strong> data, there are <strong>{1}</strong> pages, total < strong>{2}</strong> Bar Data </span></div></div>", Pager. PageSize, Pager. PageCount, Pager. TotalCount); +         returnmvchtmlstring.create (sbhtmltext.tostring ()); -}

Pager V2.0
1     /// <summary>2     ///Pager V2.03     /// </summary>4     /// <param name= "helper" >HtmlHelper</param>5     /// <param name= "Pager" >Paging Information</param>6     /// <param name= "Onpagechange" >Page Turn Event</param>7     /// <returns></returns>8      Public StaticMvchtmlstring Pager ( ThisHtmlHelper Helper, Pagermodel Pager, func<int,string>Onpagechange)9     {TenStringBuilder Sbhtmltext =NewStringBuilder (); OneSbhtmltext.append ("<div style=\ "text-align:center\" ><nav><ul class=\ "pagination\" >"); A         if(Pager. PageIndex <=1) -         { -Sbhtmltext.append ("<li class=\ "disabled\" ><a href=\ "javascript:void (0) \" aria-label=\ "previous\" ><span Aria-hidden =\ "True\" >&laquo;</span></a></li>"); the         } -         Else -         { -Sbhtmltext.appendformat ("<li><a href=\ "{0}\" aria-label=\ "previous\" ><span aria-hidden=\ "true\" >&laquo;</span ></a></li>", Onpagechange (pager. PageIndex-1)); +Sbhtmltext.appendformat ("<li><a href=\ "{0}\" >{1}</a></li>", Onpagechange (pager. PageIndex-1), Pager. PageIndex-1); -         } +Sbhtmltext.appendformat ("<li class=\ "active\" ><a href=\ "javascript:void (0) \" >{0}<span class=\ "sr-only\" > (current) </span></a></li>", Pager. PageIndex); A         if(Pager. PageIndex >=Pager. PageCount) at         { -Sbhtmltext.append ("<li class=\ "disabled\" ><a href=\ "javascript:void (0) \" aria-label=\ "next\" ><span aria-hidden=\ " True\ ">&raquo;</span></a></li>"); -         } -         Else -         { -Sbhtmltext.appendformat ("<li><a href=\ "{0}\" >{1}</a></li>", Onpagechange (pager. PageIndex +1), Pager. PageIndex +1); inSbhtmltext.appendformat ("<li><a href=\ "{0}\" aria-label=\ "next\" ><span aria-hidden=\ "true\" >&raquo;</span> </a></li>", Onpagechange (pager. PageIndex +1)); -         } toSbhtmltext.append ("</ul></nav>"); +Sbhtmltext.appendformat ("<div><span> Each page has <strong>{0}</strong> data, there are <strong>{1}</strong> pages, total < strong>{2}</strong> Bar Data </span></div></div>", Pager. PageSize, Pager. PageCount, Pager. TotalCount); -         returnmvchtmlstring.create (sbhtmltext.tostring ()); the}

Pager V3.0
1     /// <summary>2     ///Pager V3.03     /// </summary>4     /// <param name= "helper" >HtmlHelper</param>5     /// <param name= "Pager" >Paging Information</param>6     /// <param name= "Onpagechange" >page turn address or event</param>7     /// <param name= "Pagerviewname" >pagination Partial View name</param>8     /// <param name= "DisplayMode" >pagination display Mode</param>9     /// <returns></returns>Ten      Public StaticMvchtmlstring Pager ( ThisHtmlHelper Helper, Pagermodel Pager, func<int,string> Onpagechange,stringPagerviewname, Pagingdisplaymode DisplayMode =pagingdisplaymode.always) One     { APager. Onpagechange =Onpagechange; -Pager. Pagingdisplaymode =DisplayMode; -         returnmvchtmlstring.create (Helper. Partial (Pagerviewname, pager). Tohtmlstring ()); the}

Summary

Pager V1.0 pagination Style and page flipping events if you want to modify the CSharp code

Pager V2.0 can customize page flipping events, but to modify the paging style or to modify the CSharp code

Pager V3.0 can modify page flipping events or address and pagination styles in custom pagination partial view

End

Now that you've packaged this project Mvcsimplepager to NuGet http://www.nuget.org/packages/MvcSimplePager/, you can search for the NuGet Package Manager in VS Mvcsimplepager To use.

Custom Mvcsimplepager Project and sample code: Https://github.com/WeihanLi/MvcSimplePager, you can customize it as needed.

ASP. NET MVC Custom pager encapsulation and optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.