Simple and easy to use ASP.net pagination class (Ajax, custom text support)

Source: Internet
Author: User
Tags bool prev

This article mainly introduces the simple and easy to use ASP.net pagination class (support Ajax, custom text), this article directly gives the realization code and the usage method, needs the friend to be possible to refer to under

It's useful to do Web sites that don't use JS UI controls.

Usage:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var ps=new pagestring (); /* Optional Parameters * PS. Setisenglish = true;//is in English (default: false) PS. Setisshowtext = true;//Whether to display pagination text (default: TRUE)//ps. Textformat= "" (Default: Span class= "Pagetext" "strong" in total "/strong": {0} article "strong" The current "/strong": {1}/{2} "/span")//ps. Setpageindexname request["PageIndex" (Default value: "PageIndex") Ps. Setisajax = false;//(default: "false")//* function parameter/int total = 10000; int pageSize = 10;   int pageIndex = Convert.ToInt32 (request["PageIndex"]); var page = Ps.   ToString (Total, pageSize, PageIndex, "/ui/pagestringtest.aspx"); Gets the page HTML output Response.Write (page);

Effect:

Code:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153, 154 155 156 157 158 159 160 161 162 163 164 Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Text.RegularExpressions;   namespace Syntacticsugar {///<summary>///* * Description: Pagination class///* Founder time: 2015-5-29///* * Modified time:-///* * Author: Sunkaixu An public class Pagestring {///<summary>///is English (default: false)///</summary> public bool Setisenglish {get; Set ///<summary>///display pagination text (default: TRUE)///</summary> public bool Setisshowtext {get; set;}///<SUMMARY&G T Style (default: "Pagination")///</summary> public string Setclassname {get; set;}///<summary>///paging parameter name (default: " PageIndex ")///</summary> public string Setpageindexname {get; set;}///<summary>///is asynchronous synchronous href= ' asynchronous Onclick=ajaxpage () (default: false)///</summary> public bool Setisajax {get; set;}  ///<summary>///Customization Character///string. Format ("{0}{1}{2}", "Total number of records", "Current page Count", "total pages")///default value: Span class= "Pagetext" "strong" a total of "/strong": {0} "strong" current "/strong ": {1}/{2} "/span"///</summary> public string Settextformat {get; set;}   public pagestring () {setisenglish = Fals E Setisshowtext = true; Settextformat = "<span class=" Pagetext "><strong> Total </strong>:{0} bars <strong> current </strong> :{1}/{2}</span> "; Setclassname = "pagination"; Setpageindexname = "PageIndex"; Setisajax = false; }  /* free style. Pagination. Click {cursor:pointer}. pagination a{text-decoration:none;border:1px solid #AAE; color: #15 b;font-size:13px;border-radius:2px;} Pagination span{color: #666; font-size:13px;display:inline-block;border:1px solid #ccc;p adding:0.2em 0.6em; Pagination span.pagetext{Border:none}. Pagination A:hover{background: #26B; color: #fff; pagination A{display: Inline-block;padding:0.2em 0.6em;} . Pagination. Page_current{background: #26B; color: #fff; border:1px solid #AAE margin-right:5px; pagination{ margin-top:20px;} . Pagination. Current.prev, pagination Current.next{color: #999; Border-color: #999;background: #fff;} * */ ///<summary>///pagination algorithm All 20 pages first previous 1 2 3 4 5 6 7 8 9 10 Next last///</summary>///<param Name = "Total" > record number </param>///<param name= "pageSize" > per page record </param>///<param name= "PageIndex" > Current page </param>///<param name= "query_string" >url parameters </param>///<returns></returns> public string ToString (int total, int pageSize, int pageIndex, string query_string) {  int allpage = 0; int next = 0 ; int pre = 0; int startcount = 0; int endcount = 0; StringBuilder pagestr = new StringBuilder (); PageIndex = PageIndex = = 0? 1:pageindex; Pagestr. AppendFormat ("<div class=" {0} ">", Setclassname); if (PageIndex < 1) {pageIndex = 1;}//Calculate the total number of pages if (pageSize!= 0) {allpage = (total/pagesize); allpage = (All% pa Gesize)!= 0? Allpage + 1:allpage); Allpage = (Allpage = = 0 1:allpage); Next = PageIndex + 1; Pre = PageIndex-1; Startcount = (PageIndex + 5) > Allpage? Allpage-9:pageindex-4;//Middle page start ordinal//middle page terminate serial number Endcount = PageIndex < 5? 10:pageindex + 5; if (Startcount < 1) {startcount = 1;}//To avoid negative output, set if less than 1 to start with ordinal 1 if (Allpage < endcount) {endcount = Allpage; The possibility of}//page +5 will produce the final output sequence number larger than the total page number, then it is necessary to control the page numbers within   bool Isfirst = PageIndex = 1; BOOL Islast = PageIndex = = Allpage;   if (setisshowtext) pagestr. AppendFormat (Settextformat, Total, PageIndex, allpage);   if (Isfirst) {pagestr. Append ("<span> home </span> <span> prev </span>"); else {pagestr. AppendFormat ("<a href=" {0}pageindex=1 "> Home </a> <a href=" {0}pageindex={1} "> Previous page </a>", Query_ string, pre); }//middle page processing, which adds time complexity, reduces space complexity for (int i = Startcount i <= endcount; i++) {bool Iscurent = PageIndex = i; if (iscurent ) {Pagestr. Append ("<a class=" page_current ">" + i + "</a>"); else {pagestr. Append ("<a href=" "+ query_string +" pageindex= "+ i +" ">" + i + "</a>");}  } if (Islast) {pagestr. AppenD ("<span> next </span> <span> last page </span>"); else {pagestr. Append ("<a href=" "+ query_string +" pageindex= "+ Next +" "> Next </a> <a href=" "+ query_string +" Pageinde x= "+ allpage +" "> Last </a>");} Pagestr. AppendFormat ("</div>"); Return Conversiondata (pagestr. ToString ()); }   private String Conversiondata (String page) {if (setisenglish) {page= page. Replace ("prev", "Previous"). Replace ("Next page", "Next"). Replace ("All", "total"). Replace ("Current", "present"). Replace ("Bar", "records"). Replace ("Home", "first"). Replace ("End", "last"); } if (Setisajax) {var matches = regex.matches (page, @ "href=" ". *?" "", regexoptions.singleline); if (matches!= null &&A mp Matches. Count > 0) {foreach (Match m in matches) {page = page. Replace (M.value, String. Format ("class=" click "onclick=" Ajaxpage (' {0} ') ", Regex.match (M.value, String. Format (@ "{0}= (d+)", Setpageindexname)). GROUPS[1]. Value)); }} return page;  }  }  }
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.