以前在研究Discuz的时候发现了这个算法,觉得我自己写不出来,就记录了一下,现在我建的站的分页都是这段代码
1 /// <summary> 2 /// 获得页码显示链接 1 2 3 4 5 6这种 3 /// </summary> 4 /// <param name="curPage">当前页数</param> 5 /// <param name="countPage">总页数</param> 6 /// <param name="url">超级链接地址(/ChildForum.aspx)</param> 7 /// <param name="extendPage">周边页码显示个数上限(6)</param> 8 /// <param name="pagetag">页码标记(pgid)</param> 9 /// <returns>页码html</returns> 10 public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage, string pagetag) 11 { 12 if (pagetag == "") 13 pagetag = "page"; 14 int startPage = 1; 15 int endPage = 1; 16 17 if (url.IndexOf("?") > 0) 18 url = url + "&"; 19 else 20 url = url + "?"; 21 22 string t1 = "<a href=\"" + url + "&" + pagetag + "=1"; 23 string t2 = "<a href=\"" + url + "&" + pagetag + "=" + countPage; 24 25 t1 += "\">«</a>"; 26 t2 += "\">»</a>"; 27 28 if (countPage < 1) 29 countPage = 1; 30 if (extendPage < 3) 31 extendPage = 2; 32 33 if (countPage > extendPage) 34 { 35 if (curPage - (extendPage / 2) > 0) 36 { 37 if (curPage + (extendPage / 2) < countPage) 38 { 39 startPage = curPage - (extendPage / 2); 40 endPage = startPage + extendPage - 1; 41 } 42 else 43 { 44 endPage = countPage; 45 startPage = endPage - extendPage + 1; 46 t2 = ""; 47 } 48 } 49 else 50 { 51 endPage = extendPage; 52 t1 = ""; 53 } 54 } 55 else 56 { 57 startPage = 1; 58 endPage = countPage; 59 t1 = ""; 60 t2 = ""; 61 } 62 63 System.Text.StringBuilder s = new System.Text.StringBuilder(""); 64 65 s.Append(t1); 66 for (int i = startPage; i <= endPage; i++) 67 { 68 if (i == curPage) 69 { 70 s.Append("<span>"); 71 s.Append(i); 72 s.Append("</span>"); 73 } 74 else 75 { 76 s.Append("<a href=\""); 77 s.Append(url); 78 s.Append(pagetag); 79 s.Append("="); 80 s.Append(i); 81 s.Append("\">"); 82 s.Append(i); 83 s.Append("</a>"); 84 } 85 } 86 s.Append(t2); 87 88 return s.ToString(); 89 }
--------------------------------------------------------------------------------------------------------------------------------------------
顺势而为
顺势而为