.Net分页控件发布

2008-02-22 09:34:28来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

由于.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("&nbsp;");
89 l.RenderControl(writer);
90 }
91
92 void RenderFirst (HtmlTextWriter writer)
93 {
94 firstButton.RenderControl(writer);
95
96 LiteralControl l = new LiteralControl("&nbsp;");
97 l.RenderControl(writer);
98 }
99
100 void RenderLast (HtmlTextWriter writer)
101 {
102 lastButton.RenderControl(writer);
103
104 LiteralControl l = new LiteralControl("&nbsp;");
105 l.RenderControl(writer);
106 }
107
108 void RenderPrevious (HtmlTextWriter writer)
109 {
110 previousButton.RenderControl(writer);
111
112 LiteralControl l = new LiteralControl("&nbsp;");
113 l.RenderControl(writer);
114 }
115
116 void RenderNext(HtmlTextWriter writer)
117 {
118 nextButton.RenderControl(writer);
119
120 LiteralControl l = new LiteralControl("&nbsp;");
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("&nbsp;");
141 }
142
143 LiteralControl l1 = new LiteralControl("&nbsp;");

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:.NET下生产图片验证码

下一篇:如何在搜索结果出来之前,让页面显示“等待中...”