ASP. net mvc pager page

Source: Internet
Author: User
Tags actionlink

3.2 simplest solution

I think the simplest thing is to write links directly. Of course, we also need to consider changing the routing rules, so we can write the simplest example:

<% Int p = 1; Int. tryparse (request. querystring ["p"], out P); %> <div> <% = html. actionlink ("Previous Page", "Index", new {P = p-1}) %> <strong> current page: <% = P %> </strong> <% = html. actionlink ("next page", "Index", new {P = p + 1}) %> </div>

In this way, you can obtain the paging Style

Of course, you can also write links on pages 1, 2, 3, 4, and 5 without "previous" or "Next"

But there is a problem with this method, that is, when using HTML. actionlink, you must use a string to specify action and controller. Next we will change to another method to implement

3.3 using routelink

We can use HTML. routelink to avoid coupling with the action or controller name, for example:

<% For (INT I = 1; I <10; I ++) {viewcontext. routedata. values ["p"] = I; // sets the page number writer. write (HTML. routelink (I. tostring (), viewcontext. routedata. values); // display the link writer after the settings page. write (""); // a space is displayed after the connection, please note} %>

This list is displayed

3.4 complete the pager and encapsulate it into a helper

Pager is listed above, but there are several problems

· No top or bottom pages

· No special display specified for the current page

· Write once each call

· If querystring has other parameters, it cannot be processed.

Then we will improve this pager.

And encapsulate it into a helper

 

/// <Summary> /// pager display /// </Summary> /// <Param name = "html"> </param> /// <Param name = "currentpagestr"> querystringkey that identifies the current page </param> // <Param name = "pagesize"> displayed on each page </param> /// <Param name =" totalcount "> total data volume </param> // <returns> </returns> Public static string Pager (this htmlhelper HTML, string currentpagestr, int pagesize, int totalcount) {var querystring = html. viewcontext. httpcontext. reques T. querystring; int currentpage = 1; // int of the current page. tryparse (querystring [currentpagestr], out currentpage); // bind var totalpages = math to the corresponding querystring. max (totalcount + pagesize-1)/pagesize, 1); // the total number of pages var dict = new routevaluedictionary (HTML. viewcontext. routedata. values); var output = new stringbuilder (); foreach (string key in querystring. keys) if (querystring [Key]! = NULL &&! String. isnullorempty (key) dict [Key] = querystring [Key]; If (totalpages> 1) {If (currentpage! = 1) {// process the home page connection dict ["p"] = 1; output. appendformat ("{0}", HTML. routelink ("Homepage", dict);} If (currentpage> 1) {// process the connection to the previous page dict ["p"] = currentpage-1; output. append (HTML. routelink ("Previous Page", dict);} else {output. append ("Previous Page");} output. append (""); int currint = 5; For (INT I = 0; I <= 10; I ++) {// a total of 10 page numbers can be displayed, first 5, followed by 5 If (currentpage + I-currint)> = 1 & (currentpage + I-currint) <= totalpages) If (currint = I) {// process output on the current page. append (string. format ("[{0}]", currentpage);} else {// General page processing dict ["p"] = currentpage + I-currint; output. append (HTML. routelink (currentpage + I-currint ). tostring (), dict);} output. append ("");} If (currentpage <totalpages) {// process the next page Link dict ["p"] = currentpage + 1; output. append (HTML. routelink ("next page", dict);} else {output. append ("next page");} output. append (" "); If (currentpage! = Totalpages) {dict ["p"] = totalpages; output. append (HTML. routelink ("last page", dict);} output. append ("");} output. appendformat ("{0}/{1}", currentpage, totalpages); // return output if this statistic is not added. tostring ();}

Effect

 

Related Article

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.