Since the paging function that comes with .net is extremely rigid and not very customizable, it cannot meet the needs. I spent more than a week writing a .net paging control
feature:
1. Support datagrid, datalist and other types of data display controls. In theory, as long as there is a datasource control, it can be used
2. Supports url paging. Each page in url paging can be searched by search engines, and supports url rewriting.
3. Control style can be customized
4. The package is complete. If you have used the program, you only need to write one line of code
1 /**************************************** *******************************
2 ** File name: Pager.cs
3 ** Founder: Yang Xiangwu
4 ** Date: July 27, 2005
5 ** Modified by:
6** Date:
7 ** Description:
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 /// Paging control
23 /// </summary>
24 public class Pager: Label, INamingContainer
25 {
26 #region member variables and constructors
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 rendering method
59 protected override void Render(HtmlTextWriter writer)
60 {
61 // No control will be displayed when the total number of pages modified is less than or equal to 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 control tree method
179 /// <summary>
180 /// Information tag
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 /// Home page last page
210 /// </summary>
211 void AddFirstLastHyperLinks()
212 {
213 firstButton = new HyperLink();
214 firstButton.ID = "First";
215 firstButton.Text = "Home";
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 = "Last page";
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 /// Previous page Next page
243 /// </summary>
244 void AddPreviousNextHyperLinks()
245 {
246 previousButton = new HyperLink();
247 previousButton.ID = "Prev";
248 previousButton.Text = "Previous page";
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 = "Next page";
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 Private properties
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 Helper methods and public properties
299
300 /// <summary>
301 /// Get the hyperlink string of the paging navigation button
302 /// </summary>
303 /// <param name="pageIndex">The page index corresponding to the paging button</param>
304 /// <returns>Hyperlink string of paging navigation button</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 /// Current page
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 ///Number of records per page
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 sets
384 {
385 ViewState["PageSize"] = value;
386 }
387
388 }
389
390 /// <summary>
391 ///Total number of records
392 /// </summary>
393 public int TotalRecords
394 {
395 get
396 {
397 return Convert.ToInt32(ViewState["TotalRecords"]);
398 }
399 sets
400 {
401 ViewState["TotalRecords"] = value;
402 }
403 }
404
405 private string text = "Total {0} pages";
406
407 private string linkCss = "";
408 /// <summary>
409 /// Link style
410 /// </summary>
411 public string LinkCss
412 {
413 get { return linkCss; }
414 set { linkCss = value;}
415 }
416
417 private string textCss = "";
418 /// <summary>
419 /// Text style
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 sets
434 {
435 text = value;
436 }
437 }
438
439 #endregion
440 }
441 }
442
Just write this on the page:
1 <%@ Control Language="c#" %>
2 <%@ Register TagPrefix="Anchor" Namespace="MyCompany.Controls" Assembly="MyCompany.Controls" %>
3 <ANCHOR:PAGER id="Pager1" runat="server"></ANCHOR:PAGER>
4