这个分页方法是我在一个开源项目里面找到的,拿过来给大家分享!
不说了,上代码!
1 #region 替换指定的字符串 2 /// <summary> 3 /// 替换指定的字符串 4 /// </summary> 5 /// <param name="originalStr">原字符串</param> 6 /// <param name="oldStr">旧字符串</param> 7 /// <param name="newStr">新字符串</param> 8 /// <returns></returns> 9 public static string ReplaceStr(string originalStr, string oldStr, string newStr) 10 { 11 if (string.IsNullOrEmpty(oldStr)) 12 { 13 return ""; 14 } 15 return originalStr.Replace(oldStr, newStr); 16 } 17 #endregion 18 19 #region 显示分页 20 /// <summary> 21 /// 返回分页页码 22 /// </summary> 23 /// <param name="pageSize">页面大小</param> 24 /// <param name="pageIndex">当前页</param> 25 /// <param name="totalCount">总记录数</param> 26 /// <param name="linkUrl">链接地址,__id__代表页码</param> 27 /// <param name="centSize">中间页码数量</param> 28 /// <returns></returns> 29 public static string PagingString(int pageIndex, int pageSize, int totalCount, string linkUrl, int centSize) 30 { 31 //计算页数 32 if (totalCount < 1 || pageSize < 1) 33 { 34 return ""; 35 } 36 int pageCount = totalCount / pageSize; 37 if (pageCount < 1) 38 { 39 return ""; 40 } 41 if (totalCount % pageSize > 0) 42 { 43 pageCount += 1; 44 } 45 if (pageCount <= 1) 46 { 47 return ""; 48 } 49 StringBuilder pageStr = new StringBuilder(); 50 string pageId = "__id__"; 51 //<li class="foo_syy">上一页</li> <li class="foo_xyy">下一页</li> 52 string firstBtn = "<li class=\"foo_syy\"><a href=\"" + ReplaceStr(linkUrl, pageId, (pageIndex - 1).ToString()) + "\">上一页</a></li>"; 53 string lastBtn = "<li class=\"foo_xyy\"><a href=\"" + ReplaceStr(linkUrl, pageId, (pageIndex + 1).ToString()) + "\">下一页</a></li>"; 54 string firstStr = "<li class=\"foo_ys\"><a href=\"" + ReplaceStr(linkUrl, pageId, "1") + "\">1</a></li>"; 55 string lastStr = "<li class=\"foo_ys\"><a href=\"" + ReplaceStr(linkUrl, pageId, pageCount.ToString()) + "\">" + pageCount.ToString() + "</a>"; 56 57 if (pageIndex <= 1) 58 { 59 firstBtn = "<li class=\"foo_syy\"><span class=\"disabled\">上一页</span></li>"; 60 } 61 if (pageIndex >= pageCount) 62 { 63 lastBtn = "<li class=\"foo_xyy\"><span class=\"disabled\">下一页</span></li>"; 64 } 65 if (pageIndex == 1) 66 { 67 firstStr = "<li class=\"foo_ys\"><span class=\"current\">1</span></li>"; 68 } 69 if (pageIndex == pageCount) 70 { 71 lastStr = "<li class=\"foo_ys\"><span class=\"current\">" + pageCount.ToString() + "</span></li>"; 72 } 73 int firstNum = pageIndex - (centSize / 2); //中间开始的页码 74 if (pageIndex < centSize) 75 firstNum = 2; 76 int lastNum = pageIndex + centSize - ((centSize / 2) + 1); //中间结束的页码 77 if (lastNum >= pageCount) 78 lastNum = pageCount - 1; 79 pageStr.Append(firstBtn + firstStr); 80 if (pageIndex >= centSize) 81 { 82 pageStr.Append("<li class=\"foo_ys\"><span>...</span></li>"); 83 } 84 for (int i = firstNum; i <= lastNum; i++) 85 { 86 if (i == pageIndex) 87 { 88 pageStr.Append("<li class=\"foo_ys\"><span class=\"current\">" + i + "</span></li>"); 89 } 90 else 91 { 92 pageStr.Append("<li class=\"foo_ys\"><a href=\"" + ReplaceStr(linkUrl, pageId, i.ToString()) + "\">" + i + "</a></li>"); 93 } 94 } 95 if (pageCount - pageIndex > centSize - ((centSize / 2))) 96 { 97 pageStr.Append("<li class=\"foo_xyy\"><span>...</span></li>"); 98 } 99 pageStr.Append(lastStr + lastBtn); 100 return pageStr.ToString(); 101 } 102 #endregion