. Net paging control released

Source: Internet
Author: User
Features:
1. Supports DataGrid and datalist data display controls. Theoretically, any datasource control can be used.

2. Supports pagination by URL. Each page of the URL page can be searched by the search engine and URL rewriting is supported.
3. Control styles can be customized
4. Completely encapsulated and used Program Write only one line of code

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 // Page Control
23 ///
24 public class Pager: Label, inamingcontainer
25 {
26 # Region member variables and constructors
27 httpcontext context = httpcontext. Current;
28 label countlabel;
29 hyperlink previusbutton;
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 addpreviusnexthyperlinks ();
53
54 addfirstlasthyperlinks ();
55}
56 # endregion
57
58 # region Rendering Method
59 protected override void render (htmltextwriter writer)
60 {
61 // do not display any controls when the total number of pages is smaller 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 ("& 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 previusbutton. 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 = "" + (I + 1). tostring () + "";
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 ;");
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 ///

180 /// Information Tag
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 <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 ///

209 /// Homepage Last page
210 ///
211 void addfirstlasthyperlinks ()
212 {
213 firstbutton = new hyperlink ();

214 firstbutton. ID = "first ";
215 firstbutton. Text = "Homepage ";
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 ///

242 // next page on the previous page
243 ///
244 void addpreviusnexthyperlinks ()
245 {
246 previusbutton = new hyperlink ();
247 previusbutton. ID = "Prev ";
248 previusbutton. Text = "Previous Page ";
249 If (hasprevious)
250 {
251 previusbutton. navigateurl = gethrefstring (pageindex-1 );
252}
253 else
254 {
255 previusbutton. Enabled = false;
256}
257 controls. Add (previusbutton );
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 attributes
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 help methods and Public Attribute
299
300 ///

301 // retrieve the page Navigation The hyperlink string of the button.
302 ///
303 ///304 /// Hyperlink string of the paging navigation button
305 private string gethrefstring (INT pageindex)
306 {
307 string url = page. Request. rawurl;
308
309 if (RegEx. ismatch (URL, @ "(\ d00000000.shtml", regexoptions. ignorecase ))
310 {
311 url = RegEx. Replace (URL, @ "(\ d00000000.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 ///

338 // current page
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 // number of records per page
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 // The total number of records
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} pages in total ";
406
407 private string linkcss = "";
408 ///

409 // link Style
410 ///
411 public string linkcss
412 {
413 get {return linkcss ;}
414 set {linkcss = value ;}
415}
416
417 private string textcss = "";
418 ///

419 // text style
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
On the page, you only need to write to the following:
1 <% @ Control Language = "C #" %>
2 <% @ register tagprefix = "anchor" namespace = "mycompany. Controls" assembly = "mycompany. Controls" %>
3 <anchor: pager id = "pager1" runat = "server"> </Anchor: pager>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.