字节写了一个创建分页条的类:
 
1 /// <summary>
2 /// 生成分页标签
3 /// </summary>
4   public class Pager
5 {
6 private int pageIndex;
7 /// <summary>
8 /// 当前请求的页码
9 /// </summary>
10   public int PageIndex
11 {
12 get { return pageIndex; }
13 private set { pageIndex = value; }
14 }
15
16 private int pageSize;
17 /// <summary>
18 /// 请求的当前页的大小
19 /// </summary>
20 public int PageSize
21 {
22 get { return pageSize; }
23 private set { pageSize = value; }
24 }
25
26 private int rowCount;
27 /// <summary>
28 /// 数据总量,可以根据这个判断出能够显示多少页
29 /// </summary>
30 public int RowCount
31 {
32 get { return rowCount; }
33 private set { rowCount = value; }
34 }
35
36 private string hrefFormat;
37 /// <summary>
38 /// 页码连接所要指向的url格式化字符串例如:aticleList.aspx?{0}&delegate=wenxue
39 /// </summary>
40 public string HrefFormat
41 {
42 get { return hrefFormat; }
43 set { hrefFormat = value; }
44 }
45 private int pageCount;
46
47 private Pager() { }
48
49 public Pager(int pageIndex, int pageSize, int rowCount)
50 {
51 //计算出总共有多少页
52 this.pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
53 //如果pageIndex<1 就让当前页码等于1,如果pageIndex大于最大页码.
54 this.PageIndex = GetPageIndex(pageIndex, pageSize, rowCount);
55
56 this.PageSize = pageSize;
57 }
58
59 public string CreatePager()
60 {
61 StringBuilder sb = new StringBuilder();
62 sb.AppendLine("<div class='pager'>");
63 if (this.PageIndex > 1)
64 {
65 sb.AppendLine("\t<a id='pre' href='" + string.Format(this.HrefFormat, "page=" + GetPageIndex(this.pageIndex - 1, this.PageSize, this.RowCount)) + "'>" + "上一页" + "</a>");
66 }
67 else
68 {
69 sb.AppendLine("\t<a id='pre' class='false'>" + "上一页" + "</a>");
70 }
71 CreatePageIndex(sb);
72 if (this.pageIndex < this.pageCount)
73 {
74 sb.AppendLine("\t<a id='next' href='" + string.Format(this.HrefFormat, "page=" + GetPageIndex(this.pageIndex + 1, this.PageSize, this.RowCount)) + "'>" + "下一页" + "</a>");
75 }
76
77 sb.AppendLine("</div>");
78 return sb.ToString();
79 }
80
81 private void CreatePageIndex(StringBuilder sb)
82 {
83 if (this.pageIndex <= 7)
84 {
85 int i = 1;
86 for (; i <= (this.pageIndex > this.pageCount ? 10 : this.pageCount); i++)
87 {
88 if (i == this.pageIndex)
89 {
90 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
91 continue;
92 }
93 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");
94 }
95 if (i < this.pageCount)
96 {
97 sb.AppendLine("...");
98 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + this.pageCount + "") + "'>" + this.pageCount + "</a>");
99
100 }
101 }
102 else if (this.PageIndex > 12 && this.pageCount > this.pageIndex + 5)
103 {
104 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=1") + "'>" + 1 + "</a>");
105 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=2") + "'>" + 2 + "</a>");
106 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=3") + "'>" + 3 + "</a>");
107 sb.AppendLine("...");
108 for (int i = this.pageIndex - 4; i <= (this.pageIndex + 4 > this.pageCount ? this.pageCount : this.pageIndex + 4); i++)
109 {
110 if (i == this.pageIndex)
111 {
112 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
113 continue;
114 }
115 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");
116 }
117 sb.AppendLine("...");
118 for (int j = this.pageCount - 2; j <= this.pageCount; j++)
119 {
120 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + j) + "'>" + j + "</a>");
121 }
122 }
123 else if (this.pageIndex > 7)
124 {
125
126 int i = this.pageIndex - 4;
127 if (i > 2)
128 {
129 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=1") + "'>" + 1 + "</a>");
130 sb.AppendLine("...");
131 }
132 for (; i <= (this.pageIndex + 4 > this.pageCount ? this.pageCount : this.pageIndex + 4); i++)
133 {
134 if (i == this.pageIndex)
135 {
136 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
137 continue;
138 }
139 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");
140
141 }
142 if (i < this.pageCount)
143 {
144 sb.AppendLine("...");
145 sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + this.pageCount + "") + "'>" + this.pageCount + "</a>");
146 }
147 }
148 }
149
150 private int GetPageIndex(int pageIndex, int pageSize, int rowCount)
151 {
152 int pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
153 if (pageIndex < 1)
154 {
155 return 1;
156 }
157 else if (pageIndex > this.pageCount)//就让他等于最大页码
158 {
159 return pageCount;
160 }
161 else
162 {
163 return pageIndex;
164 }
165 }
166 }
css:
div.pager a
{
    display:block;
    float:left;
    margin-left:3px;
    text-align:center;
	text-decoration:none;
	border:1px solid Gray;
	color:Gray;
	width:26px;
}
#pre
{
	width:80px;
}
#pre.false:hover
{
	border:1px solid Gray;
	color:Gray;
	background-Color:White;
}
#next
{
	width:80px;
}
div.pager a:hover,div.pager a.Active
{
	border:1px solid black;
	background-Color:#4b6c9e;
	color:#f9f9f9;
}
 
 posted on 2011-06-19 17:56  小段段  阅读(1591)  评论(1编辑  收藏  举报