1 #region 替换指定的字符串 2 /// 3 /// 替换指定的字符串 4 /// 5 /// 原字符串 6 /// 旧字符串 7 /// 新字符串 8 /// 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 /// 21 /// 返回分页页码 22 /// 23 /// 页面大小 24 /// 当前页 25 /// 总记录数 26 /// 链接地址,__id__代表页码 27 /// 中间页码数量 28 /// 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 //
上一页 下一页 52 string firstBtn = "
上一页"; 53 string lastBtn = "
下一页"; 54 string firstStr = "
1"; 55 string lastStr = "
" + pageCount.ToString() + ""; 56 57 if (pageIndex <= 1) 58 { 59 firstBtn = "上一页"; 60 } 61 if (pageIndex >= pageCount) 62 { 63 lastBtn = "
下一页"; 64 } 65 if (pageIndex == 1) 66 { 67 firstStr = "
1"; 68 } 69 if (pageIndex == pageCount) 70 { 71 lastStr = "
" + pageCount.ToString() + ""; 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("
..."); 83 } 84 for (int i = firstNum; i <= lastNum; i++) 85 { 86 if (i == pageIndex) 87 { 88 pageStr.Append("
" + i + ""); 89 } 90 else 91 { 92 pageStr.Append("
" + i + ""); 93 } 94 } 95 if (pageCount - pageIndex > centSize - ((centSize / 2))) 96 { 97 pageStr.Append("
..."); 98 } 99 pageStr.Append(lastStr + lastBtn);100 return pageStr.ToString();101 }102 #endregion