自定义ASP.NET分页控件

自定义ASP.NET分页控件

源码:

    public class MyPagecontroll
 2     {
 3         public int TotalCount { get; set; }//数据的总条数
 4         public int PageSize { get; set; }//每一页的显示条数
 5         public int CurrentPageIndex { get; set; }//当前是页码数,也就是说明当前是第几页
 6         public int MaxPageCount { get; set; }//用于显示当前页码条的数量
 7         public string UrlFormat { get; set; }//绘制当前的Url
 8         public int PageCount { get; set; }
 9         public MyPagecontroll()
10         {
11             PageSize = Convert.ToInt32(ConfigurationManager.ConnectionStrings["PageSize"].ConnectionString);
12             MaxPageCount = Convert.ToInt32(ConfigurationManager.ConnectionStrings["MaxPageCount"].ConnectionString);
13             PageCount = (int)Math.Ceiling((double)TotalCount / PageSize);
14 
15         }
16         public void SetCurrentPageIndex(string pn)
17         {
18             int tempPageIndex;
19             if (int.TryParse(pn, out tempPageIndex))
20             {
21                 CurrentPageIndex = tempPageIndex;
22             }
23             else
24             {
25                 CurrentPageIndex = 1;
26             }
27         }
28         public string GetPageLink(int page, string txt)
29         {
30             StringBuilder sb = new StringBuilder();
31             string url = UrlFormat.Replace("{n}", page.ToString());
32             sb.Append("<a href='").Append(url).Append(">").Append(txt).Append("</>");
33             return sb.ToString();
34         }
35         public string Render()
36         {
37             StringBuilder sb = new StringBuilder();
38             int visibleStart = CurrentPageIndex - MaxPageCount / 2;
39             if (visibleStart < 1)
40             {
41                 visibleStart = 1;
42             }
43             int viszbleEnd = visibleStart + MaxPageCount;
44             if (viszbleEnd > PageCount)
45             {
46                 viszbleEnd = PageCount;
47             }
48             if (CurrentPageIndex > 1)
49             {
50                 sb.Append(GetPageLink(1, "首页"));
51                 sb.Append(GetPageLink(CurrentPageIndex - 1, "上一页"));
52             }
53             else
54             {
55                 sb.Append("<span>首页</span>");
56                 sb.Append("<span>上一页</span>");
57             }
58             for (int i = visibleStart; i <= viszbleEnd; i++)
59             {
60                 if (i == CurrentPageIndex)
61                 {
62                     sb.Append("<span>").Append(i).Append("</span>");
63                 }
64                 else
65                 {
66                     sb.Append(GetPageLink(i, i.ToString()));
67                 }
68             }
69             if (CurrentPageIndex < PageCount)
70             {
71                 sb.Append(GetPageLink(CurrentPageIndex + 1, "下一页"));
72                 sb.Append(GetPageLink(PageCount + 1, "末页"));
73             }
74             else
75             {
76                 sb.Append("<span>下一页</span>");
77                 sb.Append("<span>末页</span>");
78             }
79             return sb.ToString();
80         }
81     }

标签