说到分页,大多数人都是想到的数据库分页,当然那个是开发必备的知识。但是现在我要给大家介绍的是自己写个页面的分页方法,就是平时看到的列表页的“上一页”,“下一页”
等。很久以前自己都是用的分页控件完成分页,但是很多时候,发现它并不能满足一些功能的时候,就得自己写了。废话不说了,上代码!
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 static string First = "<li class=\"pagesdiv_first\"><a href=\"{0}\"></a></li>";//第一页 2 static string FirstNo = "<li class=\"pagesdiv_first\"><a href=\"javascript:void(0)\"></a></li>"; 3 static string Previous = "<li class=\"pagesdiv_pre\"><a href=\"{0}\"></a></li>";//上一页 4 static string PreviousNo = "<li class=\"pagesdiv_pre\"><a href=\"javascript:void(0)\"></a></li>"; 5 static string Middle = "<li class=\"pagesdiv_com\"><a href=\"{0}\">{1}</a></li>";//当前页 6 static string MiddleNo = "<li class=\"pagesdiv_curr\">{0}</li>"; 7 static string Next = "<li class=\"pagesdiv_nex\"><a href=\"{0}\"></a></li>";//下一页 8 static string NextNo = "<li class=\"pagesdiv_nex\"><a href=\"javascript:void(0)\"></a></li>"; 9 static string Last = "<li class=\"pagesdiv_last\"><a href=\"{0}\"></a></li>";//最末页 10 static string LastNo = "<li class=\"pagesdiv_last\"><a href=\"javascript:void(0)\"></a></li>"; 11 /// <summary> 12 /// 分页方法 13 /// </summary> 14 /// <param name="PageIndex">页索引,1开始</param> 15 /// <param name="PageSize">页大小</param> 16 /// <param name="DataCount">总数据条数</param> 17 /// <param name="Len">显示分页长度</param> 18 /// <param name="CurrPage">当前页</param> 19 /// <returns></returns> 20 public static string PagingString(int PageIndex,int PageSize, int DataCount, int Len, string CurrPage) 21 { 22 StringBuilder PageString = new StringBuilder(); 23 //根据数据总条数和每页显示条数,计算出列表页数量 24 int PageCount = DataCount % PageSize == 0 ? (DataCount / PageSize) : (DataCount / PageSize + 1); 25 if (PageIndex <= 0) 26 PageIndex = 1; 27 if (PageIndex > PageCount) 28 PageIndex = PageCount; 29 30 int In = (PageIndex - 1) / Len; 31 32 //每一页的第一个编号 33 int L = In * Len + 1; 34 //每一页的最后一个编号 35 int Lent = PageCount >= (L + Len - 1) ? (L + Len - 1) : PageCount; 36 if (PageIndex == 1) 37 { 38 PageString.AppendFormat(FirstNo, 1); 39 PageString.Append(PreviousNo); 40 } 41 else 42 { 43 PageString.AppendFormat(First, string.Format(CurrPage, 1)); 44 PageString.AppendFormat(Previous, string.Format(CurrPage, (PageIndex - 1))); 45 } 46 47 if (PageIndex == PageCount) 48 { 49 if (PageCount % Len == 0) 50 L = PageCount - (Len - 1); 51 else 52 L = PageCount - (PageCount % Len - 1); 53 for (int i = L; i <= PageCount; i++) 54 { 55 if (PageIndex == i) 56 PageString.AppendFormat(MiddleNo, i); 57 else 58 PageString.AppendFormat(Middle, string.Format(CurrPage, i), i); 59 } 60 } 61 else 62 { 63 for (int i = L; i <= Lent; i++) 64 { 65 if (PageIndex == i) 66 PageString.AppendFormat(MiddleNo, i); 67 else 68 PageString.AppendFormat(Middle, string.Format(CurrPage, i), i); 69 } 70 } 71 72 73 if (PageIndex == PageCount) 74 { 75 PageString.AppendFormat(NextNo); 76 PageString.AppendFormat(LastNo); 77 } 78 else//最后一页的分页列表 79 { 80 PageString.AppendFormat(Next, string.Format(CurrPage, (PageIndex + 1))); 81 PageString.AppendFormat(Last, string.Format(CurrPage, PageCount)); 82 } 83 return PageString.ToString(); 84 }