在做網站沒用 JS UI控件時 很實用
用法:
var ps=new PageString(); /*可選參數*/ ps.SetIsEnglish = true;// 是否是英文 (默認:false) ps.SetIsShowText = true;//是否顯示分頁文字 (默認:true) //ps.TextFormat="" (默認值:《span class=\"pagetext\"》《strong》總共《/strong》:{0} 條 《strong》當前《/strong》:{1}/{2}《/span》) //ps.SetPageIndexName Request["pageIndex"](默認值:"pageIndex") ps.SetIsAjax = false;// (默認值:"false") /*函數參數*/ int total = 10000; int pageSize = 10; int pageIndex = Convert.ToInt32(Request["pageIndex"]); var page = ps.ToString(total, pageSize, pageIndex, "/UI/PageStringTest.aspx?"); //獲取 page html 輸出 Response.Write(page);
效果:
代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace SyntacticSugar { /// <summary> /// ** 描述:分頁類 /// ** 創始時間:2015-5-29 /// ** 修改時間:- /// ** 作者:sunkaixuan public class PageString { /// <summary> /// 是否是英文 (默認:false) /// </summary> public bool SetIsEnglish { get; set; } /// <summary> /// 是否顯示分頁文字(默認:true) /// </summary> public bool SetIsShowText { get; set; } /// <summary> /// 樣式 (默認:"pagination") /// </summary> public string SetClassName { get; set; } /// <summary> /// 分頁參數名 (默認:"pageIndex") /// </summary> public string SetPageIndexName { get; set; } /// <summary> /// 是否是異步 同步 href='' 異步 onclick=ajaxPage() (默認:false) /// </summary> public bool SetIsAjax { get; set; } /// <summary> /// 自定義文字 /// string.Format("{0}{1}{2}","總記錄數","當前頁數","總頁數") /// 默認值:《span class=\"pagetext\"》《strong》總共《/strong》:{0} 條 《strong》當前《/strong》:{1}/{2}《/span》 /// </summary> public string SetTextFormat { get; set; } public PageString() { SetIsEnglish = false; SetIsShowText = true; SetTextFormat = "<span class=\"pagetext\"><strong>總共</strong>:{0} 條 <strong>當前</strong>:{1}/{2}</span> "; SetClassName = "pagination"; SetPageIndexName = "pageIndex"; SetIsAjax = false; } /*免費的樣式 .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{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> /// 分頁算法<一>共20頁 首頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 末頁 /// </summary> /// <param name="total">總記錄數</param> /// <param name="pageSize">每頁記錄數</param> /// <param name="pageIndex">當前頁數</param> /// <param name="query_string">Url參數</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; } //計算總頁數 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;//中間頁起始序號 //中間頁終止序號 endcount = pageIndex < 5 ? 10 : pageIndex + 5; if (startcount < 1) { startcount = 1; } //為了避免輸出的時候產生負數,設置如果小於1就從序號1開始 if (allpage < endcount) { endcount = allpage; }//頁碼+5的可能性就會產生最終輸出序號大於總頁碼,那麼就要將其控制在頁碼數之內 bool isFirst = pageIndex == 1; bool isLast = pageIndex == allpage; if (SetIsShowText) pagestr.AppendFormat(SetTextFormat, total, pageIndex, allpage); if (isFirst) { pagestr.Append("<span>首頁</span> <span>上一頁</span>"); } else { pagestr.AppendFormat("<a href=\"{0}pageIndex=1\">首頁</a> <a href=\"{0}pageIndex={1}\">上一頁</a>", query_string, pre); } //中間頁處理,這個增加時間復雜度,減小空間復雜度 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>下一頁</span> <span>末頁</span>"); } else { pagestr.Append(" <a href=\"" + query_string + "pageIndex=" + next + "\">下一頁</a> <a href=\"" + query_string + "pageIndex=" + allpage + "\">末頁</a>"); } pagestr.AppendFormat("</div>"); return ConversionData(pagestr.ToString()); } private string ConversionData(string page) { if (SetIsEnglish) { page= page.Replace("上一頁", "Previous").Replace("下一頁", "Next").Replace("總共", "total").Replace("當前", "Current").Replace("條", "records").Replace("首頁", "First").Replace("末頁", "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; } } }