1 /// <summary> 2 /// 分页字符串帮助类 3 /// </summary> 4 public static class PageStrHelper 5 { 6 /// <summary> 7 /// 获取分页字符串 8 /// </summary> 9 /// <param name="pageindex">当前页</param> 10 /// <param name="pageMaxcount">最大条数</param> 11 /// <param name="pageSize">每页条数</param> 12 /// <returns></returns> 13 public static string CreatePageStr(int pageindex, int pageMaxcount, int pageSize) 14 { 15 int pageMax = (int)Math.Ceiling(pageMaxcount * 1.0 / 10);//最大页数 16 17 if (pageMax <= 1) 18 { 19 return ""; 20 } 21 StringBuilder sb = new StringBuilder(); 22 if (pageindex == 1) 23 { 24 sb.AppendFormat("<ul class='pagination'><li><a class='disabled page' aria-label='Previous' page='{0}'><span aria-hidden='true'>«</span></a></li>", pageindex); 25 } 26 else 27 { 28 sb.AppendFormat("<ul class='pagination'><li><a class='page' aria-label='Previous' page='{0}'><span aria-hidden='true'>«</span></a></li>", pageindex - 1); 29 } 30 31 if (pageMax <= 10 && pageMax >= 2) 32 { 33 34 for (int i = 1; i <= pageMax; i++) 35 { 36 if (i == pageindex) 37 { 38 sb.AppendFormat("<li><a class='actpage btn page' page='{0}'>{0}</a></li>", i); 39 } 40 else 41 { 42 sb.AppendFormat("<li><a class='page' page='{0}'>{0}</a></li>", i); 43 } 44 } 45 } 46 if (pageMax > 10) 47 { 48 int first = 1; 49 int end = pageMax; 50 if (pageindex >= 4 && pageindex <= pageMax - 4) 51 { 52 first = pageindex - 4; 53 end = pageindex + 4; 54 } 55 if (pageindex == 3) 56 { 57 first = 1; 58 end = 10; 59 } 60 if (pageindex == 2) 61 { 62 first = 1; 63 end = 10; 64 } 65 if (pageindex == 1) 66 { 67 first = 1; 68 end = 10; 69 } 70 if (pageindex == pageMax) 71 { 72 first = pageMax - 9; 73 end = pageMax; 74 } 75 if (pageindex == pageMax - 1) 76 { 77 first = pageMax - 9; 78 end = pageMax; 79 } 80 if (pageindex == pageMax - 2) 81 { 82 first = pageMax - 9; 83 end = pageMax; 84 } 85 if (pageindex == pageMax - 3) 86 { 87 first = pageMax - 9; 88 end = pageMax; 89 } 90 for (int i = first; i <= end; i++) 91 { 92 if (i == pageindex) 93 { 94 sb.AppendFormat("<li><a class='actpage btn page' page='{0}'>{0}</a></li>", i); 95 } 96 else 97 { 98 sb.AppendFormat("<li><a class='page' page='{0}'>{0}</a></li>", i); 99 } 100 101 } 102 } 103 104 if (pageindex == pageMax) 105 { 106 sb.AppendFormat("<li><a class='disabled page' aria-label='Next' page='{0}'><span aria-hidden='true'>»</span></a></li></ul>", pageMax); 107 } 108 else 109 { 110 sb.AppendFormat("<li><a class='page' aria-label='Next' page='{0}'><span aria-hidden='true'>»</span></a></li></ul>", pageindex + 1); 111 } 112 113 return sb.ToString(); 114 } 115 } 116 }