工程是一门科学,科学是严谨的。

分页标签字符串生成方法

 1 /// <summary>
 2         /// 分页标签字符串生成
 3         /// </summary>
 4         /// <param name="pageNum">页码</param>
 5         /// <param name="pageCount">每页信息数</param>
 6         /// <param name="amount">信息总数</param>
 7         /// <param name="url">页面URL(会在后面拼接?pn=页码)</param>
 8         /// <returns></returns>
 9         public static string PageTagString(int pageNum, int pageCount, int amount, string url)
10         {
11             StringBuilder str = new StringBuilder();
12             string connStr = string.Empty;
13             if (amount > pageCount)
14             {
15                 if (url.IndexOf("pn=") > 0)
16                 {
17                     if (url.IndexOf("&", url.IndexOf("pn=")) > 0)
18                     {
19                         string tmp = url.Substring(url.IndexOf("pn="), url.IndexOf("&", url.IndexOf("pn=")) - url.IndexOf("pn=") + 1);
20                         url = url.Replace(tmp, "");
21                     }
22                     else
23                     {
24                         string tmp = url.Substring(url.IndexOf("pn=") - 1);
25                         url = url.Replace(tmp, "");
26                     }
27                 }
28 
29                 if (url.IndexOf('?') > 0)
30                     connStr = "&";
31                 else
32                     connStr = "?";
33                 int maxPn = amount / pageCount;
34                 if (amount % pageCount > 0) maxPn++;
35                 int prevPn = pageNum - 1;
36                 if (prevPn <= 0) prevPn = 1;
37                 int nextPn = pageNum + 1;
38                 if (nextPn > maxPn) nextPn = maxPn;
39 
40                 if (pageNum > 3 && maxPn > 5)
41                 {
42                     str.Append("<a href=\"" + url + connStr + "pn=1\">首页</a>");
43                     str.Append("<a href=\"" + url + connStr + "pn=" + (pageNum - 1 < 1 ? 1 : pageNum - 1) + "\">上一页</a>");
44                     str.Append("...");
45                 }
46                 else if (pageNum > 1)
47                 {
48                     str.Append("<a href=\"" + url + connStr + "pn=" + (pageNum - 1 < 1 ? 1 : pageNum - 1) + "\">上一页</a>");
49                 }
50 
51                 if (pageNum > 3)
52                 {
53                     for (int i = pageNum - 2; i <= (pageNum + 2 > maxPn ? maxPn : pageNum + 2); i++)
54                     {
55                         if (i == pageNum)
56                         {
57                             str.Append("<span>" + i + "</span>");
58                         }
59                         else
60                         {
61                             str.Append("<a href=\"" + url + connStr + "pn=" + i + "\">" + i + "</a>");
62                         }
63                     }
64                 }
65                 else
66                 {
67                     for (int i = 1; i <= (maxPn < 5 ? maxPn : 5); i++)
68                     {
69                         if (i == pageNum)
70                         {
71                             str.Append("<span>" + i + "</span>");
72                         }
73                         else
74                         {
75                             str.Append("<a href=\"" + url + connStr + "pn=" + i + "\">" + i + "</a>");
76                         }
77                     }
78                 }
79                 if (maxPn - pageNum > 2 && maxPn > 5)
80                 {
81                     str.Append("...");
82                     str.Append("<a href=\"" + url + connStr + "pn=" + (pageNum + 1 > maxPn ? pageNum : pageNum + 1) + "\">下一页</a>");
83                     str.Append("<a href=\"" + url + connStr + "pn=" + maxPn + "\">末页</a>");
84                 }
85                 else if (pageNum < maxPn)
86                 {
87                     str.Append("<a href=\"" + url + connStr + "pn=" + (pageNum + 1 > maxPn ? pageNum : pageNum + 1) + "\">下一页</a>");
88                 }
89             }
90             return str.ToString();
91         }

 CSS:

1 /*翻页*/
2 .pager {text-align:center; padding:20px 0 10px 0;}
3 .pager a , .pager span{border: 1px solid #CCCCCC; border-radius: 2px 2px 2px 2px; color: #004eff; padding: 4px 12px;}
4 .pager a:hover, .pager span,.pager a.next:hover {background: none repeat scroll 0 0 #E6E6E6; border: 1px solid #CCCCCC; border-radius: 2px 2px 2px 2px;color: #000000;text-decoration: none;}
5 .pager a.next {background: none repeat scroll 0 0 ; border: 1px solid #CCCCCC; border-radius: 2px 2px 2px 2px;color: #004eff;}

 

posted @ 2013-02-01 14:44  大圣的笑  阅读(286)  评论(0编辑  收藏  举报