一个简单的分页控件,内置4种显示风格,主要是将AspNetForums的分页跟一个朋友(小熊)的合一块了,ANF的单调了点,小熊的吧,用起来有点麻烦,我人懒,就整一块了.:)
内置四种风格,通过设置StyleName(BasePager,CoolPager,DiscuzPager,SmallPager)就行,不过要注意大小写,CSS是从Assembly里面取的.
下面是效果,
如果想定制一些更炫的效果,就将StyleName设置成其它的任意名字.自己写CSS:
生成的HTML代码如下:
<script type="text/javascript">
//<![CDATA[
function xiaokui_pager_go(){
var n=event.srcElement;
if(event.keyCode==13)
{
var strUrl=location.href.toLowerCase();
strUrl=strUrl.replace(/(\&)?pageindex=\d+/gi,'');
if(location.search=='')
strUrl=strUrl+'?pageindex='+n.value;
else
strUrl=strUrl+'&pageindex='+n.value;
strUrl=strUrl.replace('?&','?');
location.href=strUrl;
return false;
}
}//]]>
</script>
<div id="Pager1" class="SmallPager Left">
<span class="SmallPager_pre">
<a id="Pager1" class="" id="Prev" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=1"><</a>
</span>
<span class="SmallPager_curr">1</span>
<span class="SmallPager_number"><a id="2" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=2">2</a></span>
<span class="SmallPager_number"><a id="3" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=3">3</a></span>
<span class="SmallPager_number"><a id="4" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=4">4</a></span>
<span class="SmallPager_number"><a id="5" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=5">5</a></span>
<span class="SmallPager_next"><a id="Next" href="http://localhost:2625/WebSite/Default.aspx?PageIndex=2">></a></span>
GO:<input name="txtCustom" type="text" id="txtCustom" onkeydown="return xiaokui_pager_go();" onkeyup="this.value=this.value.replace(/[^0-9]/gi,'');" />
</div>
都在Span里面包着.想怎么改,就怎么改.
代码如下:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Text;
5using System.Web;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.IO;
9using System.Reflection;
10
11namespace Bqrm.Web.UI.WebControls
12{
13 对齐方式 enum#region 对齐方式 enum
14 /**//// <summary>
15 /// 对齐方式
16 /// </summary>
17 public enum Align
18 {
19 /**//// <summary>
20 /// 左对齐
21 /// </summary>
22 Left = 0,
23 /**//// <summary>
24 /// 居中
25 /// </summary>
26 Center,
27 /**//// <summary>
28 /// 右对齐
29 /// </summary>
30 Right
31 }
32 #endregion
33 /**//// <summary>
34 /// 分页显示类
35 /// </summary>
36 [ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
37 public class Pager:WebControl
38 {
39
40 私有成员#region 私有成员
41 private int _PageIndex;
42 private string _StyleName;
43 private Align _Align;
44 private StringBuilder sbData = new StringBuilder();
45 HyperLink previousLink;
46 HyperLink nextLink;
47 HyperLink firstLink;
48 HyperLink lastLink;
49 HyperLink[] pagingHyperLinks;
50 TextBox txtCustom;
51 private string preString = "<span class=\"{0}_{1}\">";
52 private string lastString = "</span>";
53 private string currentPageString = "<span class=\"{0}_curr\">{1}</span>";
54 private bool showTextBox = true;
55 #endregion
56
57 构造函数#region 构造函数
58 /**//// <summary>
59 /// 构造函数
60 /// </summary>
61 public Pager()
62 {
63 this._StyleName = "BasePager";
64 this._Align = Align.Left;
65 }
66 #endregion
67
68 bool showCurrentPage = true;
69 /**//// <summary>
70 /// 显示当前页统计信息.如:当前页 1/10 共有1000条记录
71 /// </summary>
72 public bool ShowCurrentPage
73 {
74 get { return showCurrentPage; }
75 set { showCurrentPage = value; }
76 }
77
78 string firstPageText = "<<";
79 /**//// <summary>
80 /// 首页链接显示的字符
81 /// </summary>
82 public string FirstPageText
83 {
84 get { return firstPageText; }
85 set { firstPageText = value; }
86 }
87
88 string lastPageText = ">>";
89 /**//// <summary>
90 /// 尾页链接显示的字符
91 /// </summary>
92 public string LastPageText
93 {
94 get { return lastPageText; }
95 set { lastPageText = value; }
96 }
97
98 string previousPageText = "<";
99 /**//// <summary>
100 /// 上一页链接显示的字符
101 /// </summary>
102 public string PreviousPageText
103 {
104 get { return previousPageText; }
105 set { previousPageText = value; }
106 }
107
108 string nextPageText = ">";
109 /**//// <summary>
110 /// 下一页链接显示的字符.默认">"
111 /// </summary>
112 public string NextPageText
113 {
114 get { return nextPageText; }
115 set { nextPageText = value; }
116 }
117
118 /**//// <summary>
119 /// 当前页码
120 /// </summary>
121 public virtual int PageIndex
122 {
123 get
124 {
125 HttpContext context = HttpContext.Current;
126
127 if (Page.IsPostBack && ViewState["PageIndex"] != null)
128 {
129 _PageIndex = (int)ViewState["PageIndex"];
130 }
131 else
132 {
133 int.TryParse(Page.Request.QueryString["pageindex"], out this._PageIndex);
134 if (this._PageIndex == 0)
135 this._PageIndex = 1;
136 }
137
138 if (_PageIndex < 0)
139 return 0;
140 else
141 return _PageIndex;
142 }
143 set
144 {
145 ViewState["PageIndex"] = value;
146 _PageIndex = value;
147 }
148 }
149
150 /**//// <summary>
151 /// 一页显示的数据条数
152 /// </summary>
153 public virtual int PageSize
154 {
155 get
156 {
157 int pageSize = Convert.ToInt32(ViewState["PageSize"]);
158 if (pageSize == 0) return 10;
159 return pageSize;
160 }
161 set
162 {
163 ViewState["PageSize"] = value;
164 }
165 }
166
167 /**//// <summary>
168 /// 总记录数
169 /// </summary>
170 public int TotalRecord
171 {
172 get
173 {
174 return Convert.ToInt32(ViewState["TotalRecords"]);
175 }
176 set
177 {
178 ViewState["TotalRecords"] = value;
179 }
180 }
181 /**//// <summary>
182 /// 生成指定页码的链接地址
183 /// </summary>
184 /// <param name="pageIndex"></param>
185 /// <returns></returns>
186 protected virtual string CreatePagerURL(string pageIndex)
187 {
188 HttpContext context = HttpContext.Current;
189 if (context.Request.Url.AbsoluteUri.IndexOf("?") == -1)
190 {
191 return context.Request.Url.AbsoluteUri.ToString() + "?PageIndex=" + pageIndex;
192 }
193 else
194 {
195 if (context.Request.Url.AbsoluteUri.ToLower().IndexOf("pageindex=") == -1)
196 return context.Request.Url.AbsoluteUri.ToString() + "&PageIndex=" + pageIndex;
197 else
198 {
199 return System.Text.RegularExpressions.Regex.Replace(context.Request.Url.AbsoluteUri.ToLower().ToString(), @"pageindex=(\d+\.?\d*|\.\d+)", "PageIndex=" + pageIndex);
200 }
201 }
202 }
203
204 /**//// <summary>
205 /// 总页数
206 /// </summary>
207 protected int TotalPage
208 {
209 get { return this.CalculateTotalPages(); }
210 }
211 /**//// <summary>
212 /// 样式
213 /// </summary>
214 public string StyleName
215 {
216 set { _StyleName = value; }
217 get { return _StyleName; }
218 }
219 /**//// <summary>
220 /// 对齐方式
221 /// </summary>
222 public Align HAlign
223 {
224 set { _Align = value; }
225 }
226 /**//// <summary>
227 /// 显示页码输入框
228 /// </summary>
229 public bool ShowTextBox
230 {
231 get { return showTextBox; }
232 set { showTextBox = value; }
233 }
234
235 protected override void CreateChildControls()
236 {
237 Controls.Clear();
238 AddPageLinks();
239 AddPreviousNextLinks();
240 AddFirstLastLinks();
241 AddCustomPager();
242 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "customPager", GetScripts(), true);
243 }
244 String GetScripts()
245 {
246 StringBuilder sb = new StringBuilder();
247 sb.Append(@"
248 function xiaokui_pager_go(){
249 var n=event.srcElement;
250 if(event.keyCode==13)
251 {
252 var strUrl=location.href.toLowerCase();
253 strUrl=strUrl.replace(/(\&)?pageindex=\d+/gi,'');
254 if(location.search=='')
255 strUrl=strUrl+'?pageindex='+n.value;
256 else
257 strUrl=strUrl+'&pageindex='+n.value;
258 strUrl=strUrl.replace('?&','?');
259 location.href=strUrl;
260 return false;
261 }
262 }");
263 return sb.ToString();
264 }
265
266 void AddCustomPager()
267 {
268 txtCustom = new TextBox();
269 txtCustom.ID = "txtCustom";
270 txtCustom.EnableViewState = false;
271 txtCustom.Attributes.Add("onkeydown", "return xiaokui_pager_go();");
272 txtCustom.Attributes.Add("onkeyup", "this.value=this.value.replace(/[^0-9]/gi,'');");
273 Controls.Add(txtCustom);
274 }
275 protected override void OnInit(EventArgs e)
276 {
277 base.OnInit(e);
278 AddStyle();
279 }
280 protected override void OnLoad(EventArgs e)
281 {
282 base.OnInit(e);
283 if (this.Context.Request["pagerstylename"] != null)
284 {
285 Assembly executingAssembly = Assembly.GetExecutingAssembly();
286 Stream stream = executingAssembly.GetManifestResourceStream("Bqrm." + StyleName + ".css");
287 stream.Position = 0L;
288 string script = new StreamReader(stream).ReadToEnd();
289 Page.Response.Write(script);
290 Page.Response.End();
291 }
292 }
293
294 protected void AddStyle()
295 {
296 string str2 = this.Context.Request.Url.ToString();
297 str2 = str2 + ((str2.IndexOf("?") >= 0) ? "&" : "?");
298 string s = StyleName.ToLower();
299 if (s == "basepager" || s == "coolpager" || s == "discuzpager" || s == "smallpager")
300 {
301 try
302 {
303 Page.Header.Controls.Add(new LiteralControl("<link rel=\"stylesheet\" href=\"" + str2 + "PagerStyleName=" + StyleName + "\" type=\"text/css\" />"));
304 }
305 catch
306 {
307 Page.Response.Write("<link rel=\"stylesheet\" href=\"" + str2 + "PagerStyleName=" + StyleName + "\" type=\"text/css\" />");
308 }
309 }
310 }
311
312 protected override void Render(HtmlTextWriter writer)
313 {
314 writer.Write("<div id=\"" + this.ClientID + "\" class=\"" + this._StyleName + " " + this._Align + "\">");
315 int totalPages = CalculateTotalPages();
316
317 if (totalPages <= 1)
318 return;
319
320 if (ShowCurrentPage)
321 RenderCurrentPage(writer);
322
323 AddAttributesToRender(writer);
324
325 writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
326 RenderFirst(writer);
327
328 RenderPrevious(writer);
329
330 RenderPagingButtons(writer);
331
332 RenderNext(writer);
333
334 RenderLast(writer);
335
336 RenderCustom(writer);
337
338 writer.Write("</div>");
339
340 }
341 void RenderCustom(HtmlTextWriter writer)
342 {
343 writer.Write(" GO:");
344 txtCustom.RenderControl(writer);
345 }
346
347 void RenderCurrentPage(HtmlTextWriter writer)
348 {
349 writer.Write(String.Format(preString, StyleName, "_total"));
350 writer.Write(string.Format(this.CurrentPageStringFormat, this.PageIndex, this.CalculateTotalPages(), this.TotalRecord));
351 writer.Write(lastString);
352 }
353
354 void RenderFirst(HtmlTextWriter writer)
355 {
356 int totalPages = CalculateTotalPages();
357
358 if ((PageIndex >= 3) && (totalPages > 5))
359 {
360 writer.Write(String.Format(preString,this.StyleName,"first"));
361 firstLink.RenderControl(writer);
362 writer.Write(lastString);
363 }
364 }
365
366 void RenderLast(HtmlTextWriter writer)
367 {
368 int totalPages = CalculateTotalPages();
369
370 if (((PageIndex + 3) < totalPages) && (totalPages > 5))
371 {
372 writer.Write(String.Format(preString,this.StyleName,"last"));
373 lastLink.RenderControl(writer);
374 writer.Write(lastString);
375 }
376 }
377
378 void RenderPrevious(HtmlTextWriter writer)
379 {
380 Literal l;
381
382 if (HasPrevious)
383 {
384 writer.Write(String.Format(preString,this.StyleName,"pre"));
385 previousLink.RenderControl(writer);
386 writer.Write(lastString);
387 }
388 }
389
390 void RenderNext(HtmlTextWriter writer)
391 {
392 if (HasNext)
393 {
394 writer.Write(String.Format(preString,this.StyleName,"next"));
395 nextLink.RenderControl(writer);
396 writer.Write(lastString);
397 }
398 }
399
400 void RenderButtonRange(int start, int end, HtmlTextWriter writer)
401 {
402 for (int i = start; i < end; i++)
403 {
404 if (PageIndex == i+1)
405 {
406 writer.Write(String.Format(currentPageString,this.StyleName,(i+1).ToString()));
407 }
408 else
409 {
410 writer.Write(String.Format(preString,this.StyleName,"number"));
411 pagingHyperLinks[i].RenderControl(writer);
412 writer.Write(lastString);
413 }
414 }
415 }
416
417 void RenderPagingButtons(HtmlTextWriter writer)
418 {
419 int totalPages;
420
421 totalPages = CalculateTotalPages();
422
423 if ( totalPages <= 5)
424 {
425 RenderButtonRange(0, totalPages, writer);
426 }
427 else
428 {
429 int lowerBound = (PageIndex - 3);
430 int upperBound = (PageIndex + 2);
431
432 if (lowerBound <= 0)
433 {
434 lowerBound = 0;
435 upperBound = 5;
436 }
437
438 if (PageIndex < (totalPages - 2))
439 RenderButtonRange(lowerBound, upperBound, writer);
440
441 else if (PageIndex >= (totalPages - 2))
442 RenderButtonRange((totalPages - 5), totalPages, writer);
443 }
444 }
445
446 void AddPageLinks()
447 {
448 pagingHyperLinks = new HyperLink[CalculateTotalPages()];
449
450 for (int i = 0; i < pagingHyperLinks.Length; i++)
451 {
452 pagingHyperLinks[i] = new HyperLink();
453 pagingHyperLinks[i].EnableViewState = false;
454 pagingHyperLinks[i].Text = (i + 1).ToString();
455 pagingHyperLinks[i].ID = (i + 1).ToString();
456 pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1).ToString());
457 Controls.Add(pagingHyperLinks[i]);
458 }
459 }
460
461 void AddFirstLastLinks()
462 {
463 int start = 1;
464 firstLink = new HyperLink();
465 firstLink.ID = "First";
466 firstLink.Text = this.FirstPageText;
467 firstLink.NavigateUrl = CreatePagerURL(start.ToString());
468 Controls.Add(firstLink);
469
470 int last = CalculateTotalPages();
471 lastLink = new HyperLink();
472 lastLink.ID = "Last";
473 lastLink.Text = this.LastPageText;
474 lastLink.NavigateUrl = CreatePagerURL(last.ToString());
475 Controls.Add(lastLink);
476 }
477
478 void AddPreviousNextLinks()
479 {
480 int previous;
481
482 if (this.PageIndex < 2)
483 previous = 1;
484 else
485 previous = this.PageIndex-1;
486
487 previousLink = new HyperLink();
488 previousLink.ID = "Prev";
489 previousLink.Text = this.PreviousPageText;
490 previousLink.NavigateUrl = CreatePagerURL(previous.ToString());
491 Controls.Add(previousLink);
492
493 int next = this.PageIndex + 1;
494 nextLink = new HyperLink();
495 nextLink.ID = "Next";
496 nextLink.Text = this.NextPageText;
497 nextLink.NavigateUrl = CreatePagerURL(next.ToString());
498 Controls.Add(nextLink);
499 }
500
501 /**//// <summary>
502 /// Override how this control handles its controls collection
503 /// </summary>
504 public override ControlCollection Controls
505 {
506 get
507 {
508 EnsureChildControls();
509 return base.Controls;
510 }
511
512 }
513
514 private bool HasPrevious
515 {
516 get
517 {
518 if (PageIndex > 0)
519 return true;
520
521 return false;
522 }
523 }
524
525 private bool HasNext
526 {
527 get
528 {
529 if (PageIndex + 1 < CalculateTotalPages() )
530 return true;
531
532 return false;
533 }
534 }
535
536 /**//// <summary>
537 /// 计算总页数
538 /// </summary>
539 ///
540 public virtual int CalculateTotalPages()
541 {
542 int totalPagesAvailable;
543
544 if (TotalRecord == 0)
545 return 0;
546
547 totalPagesAvailable = TotalRecord / PageSize;
548
549 if ((TotalRecord % PageSize) > 0)
550 totalPagesAvailable++;
551
552 return totalPagesAvailable;
553 }
554
555 string currentPageStringFormat = "当前页{0}/{1} 共有{2}条记录";// = "Page {0} of {1} ({2} records) - ";
556 public string CurrentPageStringFormat
557 {
558 get { return currentPageStringFormat; }
559 set { currentPageStringFormat = value; }
560 }
561 }
562}
563
如果有比我还懒的人,可以直接下载下面的DLL:
/Files/bqrm/BqrmPager.rar