网友花了一个多星期的时间 写成的一个.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 ///
22 /// 分页控件
23 ///
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= (totalPages - 5))
172 RenderButtonRange((totalPages - 10), totalPages, writer);
173 }
174 }
175
176 #endregion
177
178 #region 控件树方法
179 ///
180 /// 信息标签
181 ///
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; i0)
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 ///
242 /// 上一页下一页
243 /// 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 + 10)
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 10)
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 10)
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 10)
332 totalPagesAvailable++;
333
334 return totalPagesAvailable;
335 }
336
337 ///
338 /// 当前页
339 ///
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 ///
368 /// 每页记录数
369 ///
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 ///
391 /// 总记录数
392 ///
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 ///
409 /// 链接样式
410 ///
411 public string LinkCss
412 {
413 get { return linkCss; }
414 set { linkCss = value;}
415 }
416
417 private string textCss = "";
418 ///
419 /// 文字样式
420 ///
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>
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 ///
22 /// 分页控件
23 ///
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
172 RenderButtonRange((totalPages - 10), totalPages, writer);
173 }
174 }
175
176 #endregion
177
178 #region 控件树方法
179 ///
180 /// 信息标签
181 ///
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
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 ///
242 /// 上一页下一页
243 /// 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
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 1
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 1
280 return true;
281
282 return false;
283 }
284 }
285
286 private bool HasNext
287 {
288 get
289 {
290 if (PageIndex + 1
332 totalPagesAvailable++;
333
334 return totalPagesAvailable;
335 }
336
337 ///
338 /// 当前页
339 ///
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 ///
368 /// 每页记录数
369 ///
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 ///
391 /// 总记录数
392 ///
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 ///
409 /// 链接样式
410 ///
411 public string LinkCss
412 {
413 get { return linkCss; }
414 set { linkCss = value;}
415 }
416
417 private string textCss = "";
418 ///
419 /// 文字样式
420 ///
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>
浙公网安备 33010602011771号