The easy-to-use ASP. NET paging class is simple and easy to use.
It is useful when JavaScript UI controls are not used for websites
Usage:
Var ps = new PageString ();/* optional parameter */ps. setIsEnglish = true; // whether it is English (default: false) ps. setIsShowText = true; // whether to display paging text (default: true) // ps. textFormat = "" (default value: "span class = \" pagetext \ "strong" Total "/strong": {0} "strong" current "/strong": {1}/{2}/span) // ps. setPageIndexName Request ["pageIndex"] (default value: "pageIndex") ps. setIsAjax = false; // (default value: "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? "); // Get the page html OUTPUT Response. Write (page );
Effect:
Code:
Using System; using System. collections. generic; using System. linq; using System. text; using System. text. regularExpressions; namespace SyntacticSugar {// <summary> // ** Description: Paging class // ** Creation Time: // ** modification time: -/// ** Author: sunkaixuan public class PageString {// <summary> // whether it is in English (default: false) /// </summary> public bool SetIsEnglish {get; set ;}/// <summary> // whether to display paging text (default: true) /// </summary> publi C bool SetIsShowText {get; set ;}/// <summary >/// style (default: "pagination") /// </summary> public string SetClassName {get; set ;}/// <summary> /// name of the paging parameter (default: "pageIndex") /// </summary> public string SetPageIndexName {get; set ;} /// <summary> /// whether the href = ''asynchronous onclick = ajaxPage () (default: false) /// </summary> public bool SetIsAjax {get; set ;}//< summary> /// custom text /// string. format ("{0} {1 }{ 2} "," Total number of records "," Current page number "," total page number ") // default value: span class = \ "pagetext \" "strong" Total/strong: {0} strong current/strong: {1}/{2}/span /// </summary> public string SetTextFormat {get; set;} public PageString () {SetIsEnglish = false; SetIsShowText = true; setTextFormat = "<span class = \" pagetext \ "> <strong> total </strong>: {0} <strong> current </strong>: {1}/{2} </span> "; SetClassName =" pagination "; SetPageIndexName =" pag EIndex "; SetIsAjax = false;}/* free style. pagination. click {cursor: pointer }. pagination a {text-decoration: none; border: 1px solid # AAE; color: # 15B; font-size: 13px; border-radius: 2px ;}. pagination span {color: #666; font-size: 13px; display: inline-block; border: 1px solid # ccc; padding: 0.2em 0.6em ;}. pagination span. pagetext {border: none }. pagination a: hover {background: # 26B; color: # fff ;}. pagination a {d Isplay: 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> /// paging algorithm <1> A total of 20 pages: homepage 1 1 2 3 4 5 6 7 8 9 10 next last page /// </summary> /// <param name = "total"> total number of records </param> // /<Param name = "pageSize"> Number of records per page </param> // <param name = "pageIndex"> current page </param> // <param name = "query_string"> Url parameter </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 = 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 = (total % pageSize )! = 0? Allpage + 1: allpage); allpage = (allpage = 0? 1: allpage) ;}next = pageIndex + 1; pre = pageIndex-1; startcount = (pageIndex + 5)> allpage? Allpage-9: pageIndex-4; // start Number of the intermediate page // end number of the intermediate page endcount = pageIndex <5? 10: pageIndex + 5; if (startcount <1) {startcount = 1 ;}// to avoid negative output, if the value is less than 1, if (allpage <endcount) {endcount = allpage;} // if the page number is + 5, the final output sequence number is greater than the total page number, therefore, it is necessary to control the number of pages bool isFirst = pageIndex = 1; bool isLast = pageIndex = allpage; if (SetIsShowText) pagestr. appendFormat (SetTextFormat, total, pageIndex, allpage); if (isFirst) {pagestr. append ("<span> homepage </span> <span> previous page </span>");} el Se {pagestr. appendFormat ("<a href = \" {0} pageIndex = 1 \ "> homepage </a> <a href = \" {0} pageIndex = {1} \ "> previous Page </a> ", query_string, pre);} // process the intermediate page. This increases the time complexity and reduces the 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 </span>");} else {pagestr. append ("<a href = \" "+ query_string +" pageIndex = "+ next +" \ "> next page </a> <a href = \" "+ query_string +" pageIndex = "+ allpage +" \ "> last page </a> ");} pagestr. appendFormat ("</div>"); return ConversionData (pagestr. toString ();} private string ConversionData (string page) {if (SetIsEnglish) {page = page. replace ("Previous Page", "Prev Ious "). replace ("Next", "Next "). replace ("total", "total "). replace ("Current", "Current "). replace ("entry", "records "). replace ("Homepage", "First "). replace ("Last page", "Last");} if (SetIsAjax) {var matches = Regex. matches (page, @ "href \ = "". *? ", RegexOptions. Singleline); if (matches! = Null & 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 ;}}}