由于.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>
新闻热点
疑难解答
图片精选