You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 rivejä
5.5 KiB

3 vuotta sitten
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Basic.Engine.Get
  7. {
  8. /// <summary>
  9. /// 获取分页代码
  10. /// </summary>
  11. public static class PagiNation
  12. {
  13. /// <summary>
  14. /// 获取分页代码(总页数,当前页)
  15. /// </summary>
  16. /// <param name="TotelPage"></param>
  17. /// <param name="NowPage"></param>
  18. public static string GetPageHtml(int TotelPage, int NowPage)
  19. {
  20. string NowUrl = HttpContext.Current.Request.RawUrl;
  21. Regex Rzz = new Regex(@"\?page=\d+|\&page=\d+", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
  22. NowUrl = Rzz.Replace(NowUrl, "", 1);
  23. if (NowUrl.ToLower().Contains("?"))
  24. {
  25. NowUrl = NowUrl + "&page=";
  26. }
  27. else
  28. {
  29. NowUrl = NowUrl + "?page=";
  30. }
  31. int prevPage = NowPage - 1;
  32. int nextPage = NowPage + 1;
  33. StringBuilder strFenye = new StringBuilder();
  34. if (TotelPage > 1)
  35. {
  36. strFenye.Append("<div id=\"PageContent\" class=\"flickr\" style=\"text-align: center;\">");
  37. strFenye.Append("<div class=\"pages\">");
  38. if (NowPage > 1)//第一页的时候 不显示上一页
  39. {
  40. strFenye.Append("<span><a href=\"" + NowUrl + prevPage + "\">«上一页</a></span>");
  41. }
  42. if (NowPage != 1)//当前页不是第一页的时候始终显示 第一页
  43. {
  44. strFenye.Append("<span><a href=\"" + NowUrl + "1\">1</a></span>");
  45. }
  46. if (NowPage >= 5)//当前页大于5时候 显示 ...
  47. {
  48. strFenye.Append("<span>...</span>");
  49. for (int i = 2; i <= NowPage - 3; i++)
  50. {
  51. strFenye.Append("<span class=\"pageyc\"><a href=\"" + NowUrl + i + "\">" + i + "</a></span>");
  52. }
  53. }
  54. if (TotelPage < 6)
  55. {
  56. #region 总页数小于5时
  57. //
  58. for (int i = 0; i < (TotelPage + 1); i++)
  59. {
  60. if (i > 0)
  61. {
  62. if (i == NowPage)
  63. {
  64. strFenye.Append("<span class=\"number\">" + i + "</span>");
  65. }
  66. else
  67. {
  68. if (i != 1)
  69. {
  70. strFenye.Append("<span><a href=\"" + NowUrl + i + "\">" + i + "</a></span>");
  71. }
  72. }
  73. }
  74. }
  75. #endregion
  76. }
  77. else
  78. {
  79. #region 总页数大于5时
  80. //
  81. int endPage = 0;
  82. if (TotelPage > NowPage + 2)
  83. {
  84. endPage = NowPage + 2;
  85. }
  86. else
  87. {
  88. endPage = NowPage;
  89. }
  90. //
  91. for (int i = NowPage - 2; i <= endPage; i++)
  92. {
  93. if (i > 0)
  94. {
  95. if (i == NowPage)
  96. {
  97. strFenye.Append("<span class=\"number\">" + i + "</span>");
  98. }
  99. else
  100. {
  101. if (i != 1 && i != TotelPage)
  102. {
  103. strFenye.Append("<span><a href=\"" + NowUrl + i + "\">" + i + "</a></span>");
  104. }
  105. }
  106. }
  107. }
  108. if (NowPage == TotelPage - 2)
  109. {
  110. strFenye.Append("<span><a href=\"" + NowUrl + (NowPage + 1) + "\">" + (NowPage + 1) + "</a></span>");
  111. }
  112. if (NowPage + 3 < TotelPage)
  113. {
  114. strFenye.Append("<span>...</span>");
  115. for (int i = NowPage + 3; i <= TotelPage - 1; i++)
  116. {
  117. strFenye.Append("<span class=\"pageyc\"><a href=\"" + NowUrl + i + "\">" + i + "</a></span>");
  118. }
  119. }
  120. if (NowPage != TotelPage)
  121. {
  122. strFenye.Append("<span><a href=\"" + NowUrl + TotelPage + "\">" + TotelPage + "</a></span>");
  123. }
  124. #endregion
  125. }
  126. if (NowPage < TotelPage)//最后一页 不显示下一页
  127. {
  128. strFenye.Append("<span><a href=\"" + NowUrl + nextPage + "\">下一页»</a></span>");
  129. }
  130. strFenye.Append("</div>");
  131. strFenye.Append("</div>");
  132. }
  133. return strFenye.ToString();
  134. }
  135. }
  136. }