I haven't written a blog for a long time ..... Recently I wrote a. net paging control and put it in the garden... If you think it is good, just like it. If it is not good, just tell me why ....
Request. QueryString ....
Parameters:
Public int currentpageindex = 0; // the current page number public int pagesize = 16; // The number of lines displayed per page public int pagecount = 0; // The number of pages public int rowscount = 320; // Total number of items public string prevtext = "Previous Page"; public string nexttext = "next page"; public string ellipsetext = "... "; // delimiter public int edgeentries = 2; // public bool prevshowalways = true on the page; // whether public bool nextshowalways = true is displayed on the previous page; // whether the buttons on the next page display public int displayentries = 6; // The following buttons are displayed: public string url; public string pagestr = "";
There is nothing to say about calculating the maximum number of pages. Just read the number of entries from the database and assign it to rowscount.
// Calculate the maximum number of pages public int NumPages () {return Convert. ToInt32 (Math. Ceiling (decimal) rowscount/pagesize ));}
The main problem is that the start and end buttons of the computation are completed here, which is equivalent to half of the results.
At first, I used the following code, but there was a problem .... If displayentries is 2n-1, the final output is 2n.
// Calculate the start and end pages based on the current page number and display number. The public int [] GetInterval () {int nehalf = Convert. toInt32 (Math. ceiling (decimal) displayentries/2); pagecount = NumPages (); int upperlimit = pagecount-displayentries; // The maximum value is upperlimit. If it is larger, the number of buttons cannot be guaranteed. Int start = currentpageindex> nehalf? Math. Max (Math. Min (currentpageindex-nehalf, upperlimit), 1): 1; int end = currentpageindex> nehalf? Math. Min (currentpageindex + nehalf-1, pagecount): Math. Min (displayentries, pagecount); return new int [] {start, end };}
The main reason is that Math. Ceiling returns an integer, while 2n-1 and 2n return the same number, which causes a problem in the number of buttons.
So, I made a difference between an odd number and an even number.
// Calculate the start and end pages based on the current page number and display number. The public int [] GetInterval () {int nehalf = Convert. toInt32 (Math. ceiling (decimal) displayentries/2); pagecount = NumPages (); int upperlimit = pagecount-displayentries + 1; int start, end; if (displayentries % 2 = 0) {start = currentpageindex> nehalf? Math. Max (Math. Min (currentpageindex-nehalf, upperlimit), 1): 1 ;}else {start = currentpageindex> nehalf? Math. Max (Math. Min (currentpageindex-nehalf + 1, upperlimit), 1): 1 ;}end = currentpageindex> nehalf? Math. Min (currentpageindex + nehalf-1, pagecount): Math. Min (displayentries, pagecount); return new int [] {start, end };}
Add button class
public string AppendItem(int pageid, string text) { string lnk; int id = pageid < 1 ? 1 : (pageid < pagecount ? pageid : pagecount); if (id == currentpageindex) { lnk = "<span class='current'>" + text + "</span>"; } else { lnk = "<a href=?" + url + "&pageid=" + id + ">" + text + "</a>"; } return lnk; }
Pagination control generation
Public string DrawLink () {// first obtain the array int [] interval = GetInterval (); // generate the previous link if (prevtext! = "" & Currentpageindex> 1 & prevshowalways) {pagestr = AppendItem (currentpageindex-1, prevtext);} // check whether a boundary page exists, if (interval [0]> 1 & edgeentries> 0) {// check whether the boundary and start are small. Draw the int end = Math button. min (edgeentries, interval [0]); // generate the boundary page if (edgeentries = interval [0]) // note that when interval [0] is equal to edgeentries, the {for (int I = 1; I <end; I ++) {pagestr = pagestr + AppendItem (I, I. toString () ;}} else {For (int I = 1; I <= end; I ++) // starts from 1, so I must be less than or equal to end, in this way, the end button will be painted on {pagestr = pagestr + AppendItem (I, I. toString () ;}}// generate the if (edgeentries + 1 <interval [0] & ellipsetext! = "") {Pagestr = pagestr + "<span>" + ellipsetext + "</span>" ;}}for (int I = interval [0]; I <= interval [1]; I ++) {pagestr = pagestr + AppendItem (I, I. toString ();} if (interval [1] <pagecount & edgeentries> 0) // end {if (pagecount-edgeentries> interval [1] & ellipsetext! = "") {Pagestr = pagestr + "<span>" + ellipsetext + "</span>" ;}int begin = Math. max (pagecount-edgeentries + 1, interval [1]); if (pagecount-edgeentries + 1 = interval [1]) {for (int I = begin + 1; I <= pagecount; I ++) {pagestr = pagestr + AppendItem (I, I. toString () ;}} else {for (int I = begin; I <= pagecount; I ++) {pagestr = pagestr + AppendItem (I, I. toString () ;}}// link to the next page Text! = "" & Currentpageindex <pagecount & nextshowalways) {pagestr = pagestr + AppendItem (currentpageindex + 1, nexttext);} return pagestr ;}
In this way, this simple. net paging control is implemented. If you have a better method, please feel free to contact me .. Hiahia ....
MSPMicrosoft Student Partners)
Projects set up by college students worldwide. It aims to encourage those who actively advocate and promote innovative practices on campus,
They are willing to actively help outstanding students of others, provide them with the most cutting-edge technical resources, and a more extensive platform for communication and self-presentation.
The MSP program is intended for all students who love technology, are brave in innovation, actively promote innovation practices, and are willing to share their learning with others. It is not limited to technical students.
If you like, please join us. Address
Http://msdn.microsoft.com/zh-cn/jj889435.aspx
This article from "Big Bear dumb" blog, please be sure to keep this source http://wlzcool.blog.51cto.com/5341447/1242752