由于.net自带的分页功能极其死板,自定义性不强不能满足需求
俺花了一个多星期的时间 写成的一个.net分页控件
特性:
1、支持datagrid,datalist等分种数据显示控件,理论上只要有datasource的控件都可以
2、支持url方式分页,url分页的每一页都可以被搜索引擎搜到,并且支持url重写
3、控件样式可自定义
4、封装完全,使用过程序只需写一行代码
1 /******************************************************************
2 ** 文件名:Pager.cs
3 ** 创建人:杨响武
4 ** 日 期:2005年7月27日
5 ** 修改人:
6 ** 日 期:
7 ** 描 述:
8 **
9 **----------------------------------------------------------------
10 ******************************************************************/
11
12 using System;
13 using System.Web.UI.WebControls;
14 using System.Web.UI.HtmlControls;
15 using System.Web;
16 using System.Web.UI;
17 using System.Text.RegularExpressions;
18
19 namespace MyCompany.Controls
20 {
21 /// <summary>
22 /// 分页控件
23 /// </summary>
24 public class Pager : Label, INamingContainer
25 {
26 #region 成员变量和构造函数
27 HttpContext context = HttpContext.Current;
28 Label countLabel;
29 HyperLink previousButton;
30 HyperLink nextButton;
31 HyperLink firstButton;
32 HyperLink lastButton;
33 HyperLink[] pagingLinkButtons;
34
35 public override ControlCollection Controls
36 {
37 get
38 {
39 EnsureChildControls();
40 return base.Controls;
41 }
42 }
43
44 protected override void CreateChildControls()
45 {
46 Controls.Clear();
47
48 AddCountLabel();
49
50 AddPageButtons();
51
52 AddPreviousNextHyperLinks();
53
54 AddFirstLastHyperLinks();
55 }
56 #endregion
57
58 #region 呈现方法
59 protected override void Render(HtmlTextWriter writer)
60 {
61 // 修改总页数小于等于1的时候不呈现任何控件
62 // int totalPages = CalculateTotalPages();
63 //
64 // if (totalPages <= 1)
65 // return;
66
67 AddAttributesToRender(writer);
68
69 writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
70
71 RenderCountLabel(writer);
72
73 RenderFirst(writer);
74
75 RenderPrevious(writer);
76
77 RenderPagingButtons(writer);
78
79 RenderNext(writer);
80
81 RenderLast(writer);
82 }
83
84 void RenderCountLabel(HtmlTextWriter writer)
85 {
86 countLabel.RenderControl(writer);
87
88 LiteralControl l = new LiteralControl(" ");
89 l.RenderControl(writer);
90 }
91
92 void RenderFirst (HtmlTextWriter writer)
93 {
94 firstButton.RenderControl(writer);
95
96 LiteralControl l = new LiteralControl(" ");
97 l.RenderControl(writer);
98 }
99
100 void RenderLast (HtmlTextWriter writer)
101 {
102 lastButton.RenderControl(writer);
103
104 LiteralControl l = new LiteralControl(" ");
105 l.RenderControl(writer);
106 }
107
108 void RenderPrevious (HtmlTextWriter writer)
109 {
110 previousButton.RenderControl(writer);
111
112 LiteralControl l = new LiteralControl(" ");
113 l.RenderControl(writer);
114 }
115
116 void RenderNext(HtmlTextWriter writer)
117 {
118 nextButton.RenderControl(writer);
119
120 LiteralControl l = new LiteralControl(" ");
121 l.RenderControl(writer);
122 }
123
124 private void RenderButtonRange(int start, int end, HtmlTextWriter writer)
125 {
126 for (int i = start; i < end; i++)
127 {
128 if (PageIndex == i)
129 {
130 Literal l = new Literal();
131 l.Text = "<font color =red>" + (i + 1).ToString() + "</font>";
132
133 l.RenderControl(writer);
134 }
135 else
136 {
137 pagingLinkButtons[i].RenderControl(writer);
138 }
139 if (i < (end - 1))
140 writer.Write(" ");
141 }
142
143 LiteralControl l1 = new LiteralControl(" ");
144 l1.RenderControl(writer);
145 }
146
147 private void RenderPagingButtons(HtmlTextWriter writer)
148 {
149 int totalPages;
150
151 totalPages = CalculateTotalPages();
152
153 if (totalPages <= 10)
154 {
155 RenderButtonRange(0, totalPages, writer);
156 }
157 else
158 {
159 int lowerBound = (PageIndex - 4);
160 int upperBount = (PageIndex + 6);
161
162 if (lowerBound <= 0)
163 lowerBound = 0;
164
165 if (PageIndex <= 4)
166 RenderButtonRange(0, 10, writer);
167
168 else if (PageIndex < (totalPages - 5))
169 RenderButtonRange(lowerBound, (PageIndex + 6), writer);
170
171 else if (PageIndex >= (totalPages - 5))
172 RenderButtonRange((totalPages - 10), totalPages, writer);
173 }
174 }
175
176 #endregion
177
178 #region 控件树方法
179 /// <summary>
180 /// 信息标签
181 /// </summary>
182 void AddCountLabel()
183 {
184 countLabel = new Label();
185 countLabel.ID = "countLabel";
186 countLabel.Text = string.Format(text, CalculateTotalPages().ToString("n0"));
187
188 Controls.Add(countLabel);
189 }
190
191 private void AddPageButtons()
192 {
193 pagingLinkButtons = new HyperLink[CalculateTotalPages()];
194
195 for (int i = 0; i < pagingLinkButtons.Length; i++)
196 {
197 pagingLinkButtons[i] = new HyperLink();
198 pagingLinkButtons[i].EnableViewState = false;
199 pagingLinkButtons[i].Text = (i + 1).ToString();
200 pagingLinkButtons[i].ID = i.ToString();
201 pagingLinkButtons[i].CssClass = linkCss;
202 pagingLinkButtons[i].NavigateUrl = GetHrefString(i);
203
204 Controls.Add(pagingLinkButtons[i]);
205 }
206 }
207
208 /// <summary>
209 /// 首页末页
210 /// </summary>
211 void AddFirstLastHyperLinks()
212 {
213 firstButton = new HyperLink();
214 firstButton.ID = "First";
215 firstButton.Text = "首页";
216 if(PageIndex != 0 && CalculateTotalPages() > 0)
217 {
218 firstButton.NavigateUrl = GetHrefString(0);
219 }
220 else
221 {
222 firstButton.Enabled = false;
223 }
224
225 Controls.Add(firstButton);
226
227 lastButton = new HyperLink();
228 lastButton.ID = "Last";
229 lastButton.Text = "末页";
230 if(PageIndex != CalculateTotalPages() - 1)
231 {
232 lastButton.NavigateUrl = GetHrefString(CalculateTotalPages() - 1);
233 }
234 else
235 {
236 lastButton.Enabled = false;
237 }
238 Controls.Add(lastButton);
239 }
240
241 /// <summary>
242 /// 上一页下一页
243 /// </summary>
244 void AddPreviousNextHyperLinks()
245 {
246 previousButton = new HyperLink();
247 previousButton.ID = "Prev";
248 previousButton.Text = "上一页";
249 if (HasPrevious)
250 {
251 previousButton.NavigateUrl = GetHrefString(PageIndex - 1);
252 }
253 else
254 {
255 previousButton.Enabled = false;
256 }
257 Controls.Add(previousButton);
258
259 nextButton = new HyperLink();
260 nextButton.ID = "Next";
261 nextButton.Text = "下一页";
262 if(HasNext)
263 {
264 nextButton.NavigateUrl = GetHrefString(PageIndex + 1);
265 }
266 else
267 {
268 nextButton.Enabled = false;
269 }
270 Controls.Add(nextButton);
271 }
272 #endregion
273
274 #region 私有属性
275 private bool HasPrevious
276 {
277 get
278 {
279 if (PageIndex > 0)
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 1 < CalculateTotalPages() )
291 return true;
292
293 return false;
294 }
295 }
296 #endregion
297
298 #region 帮助方法和公共属性
299
300 /// <summary>
301 /// 获取分页导航按钮的超链接字符串
302 /// </summary>
303 /// <param name="pageIndex">该分页按钮相对应的页索引</param>
304 /// <returns>分页导航按钮的超链接字符串</returns>
305 private string GetHrefString(int pageIndex)
306 {
307 string url = Page.Request.RawUrl;
308
309 if (Regex.IsMatch(url, @"(d+).shtml", RegexOptions.IgnoreCase))
310 {
311 url = Regex.Replace(url, @"(d+).shtml", pageIndex + ".shtml");
312 }
313
314 if (Regex.IsMatch(url, @"PageIndex=(d+)", RegexOptions.IgnoreCase))
315 {
316 url = Regex.Replace(url, @"PageIndex=(d+)", "PageIndex=" + pageIndex.ToString());
317 }
318
319 return Page.Server.UrlPathEncode(url);
320 }
321
322 public int CalculateTotalPages()
323 {
324 int totalPagesAvailable;
325
326 if (TotalRecords == 0)
327 return 0;
328
329 totalPagesAvailable = TotalRecords / PageSize;
330
331 if ((TotalRecords % PageSize) > 0)
332 totalPagesAvailable++;
333
334 return totalPagesAvailable;
335 }
336
337 /// <summary>
338 /// 当前页
339 /// </summary>
340 public int PageIndex
341 {
342 get
343 {
344 int _pageIndex = 0;
345
346 if ((!Page.IsPostBack) && (context.PageIndex > 0))
347 _pageIndex = context.PageIndex;
348 else
349 _pageIndex = Convert.ToInt32(ViewState["PageIndex"]);
350
351 if (_pageIndex < 0)
352 return 0;
353 else
354 {
355 if (_pageIndex > CalculateTotalPages()-1)
356 return CalculateTotalPages()-1;
357 else
358 return _pageIndex;
359 }
360 }
361 set
362 {
363 ViewState["PageIndex"] = value;
364 }
365 }
366
367 /// <summary>
368 /// 每页记录数
369 /// </summary>
370 public int PageSize
371 {
372 get
373 {
374 int pageSize = Convert.ToInt32(ViewState["PageSize"]);
375
376 if (pageSize == 0)
377 {
378 return 10;
379 }
380
381 return pageSize;
382 }
383 set
384 {
385 ViewState["PageSize"] = value;
386 }
387
388 }
389
390 /// <summary>
391 /// 总记录数
392 /// </summary>
393 public int TotalRecords
394 {
395 get
396 {
397 return Convert.ToInt32(ViewState["TotalRecords"]);
398 }
399 set
400 {
401 ViewState["TotalRecords"] = value;
402 }
403 }
404
405 private string text = "共{0}页";
406
407 private string linkCss = "";
408 /// <summary>
409 /// 链接样式
410 /// </summary>
411 public string LinkCss
412 {
413 get { return linkCss; }
414 set { linkCss = value;}
415 }
416
417 private string textCss = "";
418 /// <summary>
419 /// 文字样式
420 /// </summary>
421 public string TextCss
422 {
423 get { return textCss; }
424 set { textCss = value;}
425 }
426
427 public string TextFormat
428 {
429 get
430 {
431 return text;
432 }
433 set
434 {
435 text = value;
436 }
437 }
438
439 #endregion
440 }
441 }
442
在页面上只需要向下面这样写:
1 <%@ Control Language="c#" %>
2 <%@ Register TagPrefix="Anchor" Namespace="MyCompany.Controls" Assembly="MyCompany.Controls" %>
3 <ANCHOR:PAGER id="Pager1" runat="server"></ANCHOR:PAGER>
4