Release an improved Asp.net MVC paging tool class from webform

Source: Internet
Author: User
ArticleDirectory
    • 1. A paging helper class under webform
    • 2. Static aspnetmvcpager class
    • 3. query and PASS Parameters in the Controller
    • 4. Page call

In my previous blog, I once again summarized a very wide paging helper class used in webform. This article summarizes the simple paging tool class under Asp.net MVC. For more information about MVC development, see. In essence, this paging helper class directly uses the pagination tool class under webform, and then works with the htmlhelper Extension Method to slightly improve and package. All paging processes can save a lot of effortCodeIn addition, it is convenient to call webform in view (page.

 

1. A paging helper class under webform

For details about how to use the pagination tool class in webform, refer to this article.

You need to initialize the style when using it. You can add the configuration under Web. config:

 
<Deleetask> <! -- Pagination style --> <add key = "pagerstyle" value = "pagercss"/> </appsettings>

 

2. Static aspnetmvcpager class

This static class is used to add several extension methods for generating paging tags for htmlhelper in MVC:

Using system. collections. specialized; using system. text; namespace system. web. MVC {using DOTNET. common. webform; public static class aspnetmvcpager {public static string renderpager (this htmlhelper helper, int leftsize, int currentpage, int recordsperpage, int totalcount, system. web. httprequest request) {string url = generateurl (request); aspnetpagercontrol CTRL = new aspnetpagercontrol (currentpage, Recordsperpage, totalcount, leftsize, URL); string pagerstr = aspnetpagercontrol. renderpager (CTRL); Return generatepagerstring (pagerstr);} public static string renderpager (this htmlhelper helper, int leftsize, int currentpage, int recordsperpage, int totalcount, string basestring) {aspnetpagercontrol CTRL = new aspnetpagercontrol (currentpage, recordsperpage, totalcount, leftsize, basestring); s Tring pagerstr = aspnetpagercontrol. renderpager (CTRL); Return generatepagerstring (pagerstr);} public static string renderstatisticpager (this htmlhelper helper, int leftsize, int currentpage, int recordsperpage, int totalcount, system. web. httprequest request) {string url = generateurl (request); aspnetpagercontrol CTRL = new aspnetpagercontrol (currentpage, recordsperpage, totalcount, leftsize, URL ); String pagerstr = aspnetpagercontrol. renderstatisticpager (CTRL); Return generatepagerstring (pagerstr);} public static string renderstatisticpager (this htmlhelper helper, int leftsize, int currentpage, int recordsperpage, int totalcount, string basestring) {aspnetpagercontrol CTRL = new aspnetpagercontrol (currentpage, recordsperpage, totalcount, leftsize, basestring); string pagerstr = aspnetp Agercontrol. renderstatisticpager (CTRL); Return generatepagerstring (pagerstr);} Private Static string generateurl (system. web. httprequest request) {stringbuilder url = new stringbuilder (); URL. append (request. URL. absolutepath); namevaluecollection collection = request. querystring; string [] keys = collection. allkeys; int counter = 0; For (INT I = 0; I <keys. length; I ++) {If (Keys [I]. tolower () =" Pageindex ") {continue;} URL. appendformat (" {0} {1 }={ 2} ", (counter = 0? "? ":" & "), Keys [I], Collection [Keys [I]); counter ++;} return URL. tostring ();} Private Static string generatepagerstring (string pagerstr) {tagbuilder builder = new tagbuilder ("Div"); builder. idattributedotreplacement = "_"; builder. generateid (system. guid. newguid (). tostring (); builder. innerhtml = pagerstr; return builder. tostring ();}}}

 

3. query and PASS Parameters in the Controller

In the controller, sometimes the view is searched by clicking the query button (POST method), sometimes by clicking the paging link to pass parameters through the URL (get method ), therefore, control the query conditions and correct paging should be done in ctroller. The sample code in this article gives the Code quite simple, and the paging parameters can be obtained directly. You can analyze them yourself.

 

4. Page call

Similar to webform, the page is displayed by binding directly with <% = %>:

 <% = html. renderpager (pager. leftsize, pager. currentpage, pager. recordsperpg, pager. totalcount, httpcontext. current. request) %> <% = html. renderstatisticpager (pager. leftsize, pager. currentpage, pager. recordsperpg, pager. Totalcount, httpcontext. current. request) %> <% = html. renderpager (pager. leftsize, pager. currentpage, pager. recordsperpg, pager. totalcount, String. format ("{0 }? Firstname = {1} & lastname = {2} ", httpcontext. current. request. URL. absolutepath, pager. firstname, pager. lastname) %> <% = html. renderstatisticpager (pager. leftsize, pager. currentpage, pager. recordsperpg, pager. totalcount, String. format ("{0 }? Firstname = {1} & lastname = {2} ", httpcontext. Current. Request. url. absolutepath, pager. firstname, pager. lastname) %> 

In general, it is very simple to use, and the parameter and URL in it are the key points.

It should be noted that the difference between MVC and webform is that MVC overwrites the URL, which may cause a 404 error, therefore, you must note when passing parameters.

There is a picture with the truth:

 

Download the example: mvcapp

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.