In the previous article, I wrote the paging code in the user control. After careful consideration, I found that paging is nothing more than changing the current page number and getting different data.
Therefore, the click pagination event is the same, so I thought of using the delegate to bind multiple similar events and optimize the code above.
Front-end code
1 <div id = "pager">
2 page <asp: Label ID = "curPage" runat = "server" Text = "1"> </asp: Label> & nbsp;
3 <asp: LinkButton ID = "lbtfirst" runat = "server" onclick = "lbtfirst_Click"> homepage </asp: LinkButton> & nbsp;
4 <asp: LinkButton ID = "lbtPre" runat = "server" onclick = "lbtPre_Click"> previous page </asp: LinkButton> & nbsp;
5 <asp: LinkButton
6 ID = "lbtNext" runat = "server" onclick = "lbtNext_Click"> next page </asp: LinkButton> & nbsp;
7 <asp: LinkButton ID = "lblLast"
8 runat = "server" onclick = "lblLast_Click"> last page </asp: LinkButton> & nbsp;
9 </div>
The paging code in the background does not implement paging, data sources are not set, and control parameters are bound to Data.
1 using System;
2
3 namespace Pager. controls
4 {
5 public delegate void pageChangedHandler (object sender, int nPageIndex );
6 public partial class pager3: System. Web. UI. UserControl
7 {
8 public event pageChangedHandler OnPageChange;
9 protected void Page_Load (object sender, EventArgs e)
10 {
11 if (! IsPostBack)
12 {
13 state ();
14}
15}
16 # region attributes
17
18 int pageCount = 10;
19 /// <summary>
20 // Number of pages per page
21 /// </summary>
22 public int PageCount
23 {
24 get {return pageCount ;}
25 set {pageCount = value ;}
26}
27 int pagelist = 1;
28 /// <summary>
29 // page number
30 /// </summary>
31 public int Pagelist
32 {
33 get {return pagelist ;}
34 set {pagelist = value ;}
35}
36
37 private void state ()
38 {
39 if (this. curPage. Text = "1 ")
40 {
41 lbtfirst. Enabled = false;
42 lbtPre. Enabled = false;
43 lbtNext. Enabled = true;
44 lblLast. Enabled = true;
45}
46 if (int. Parse (this. curPage. Text)> 1 & int. Parse (this. curPage. Text) <Pagelist)
47 {
48 lbtfirst. Enabled = true;
49 lbtPre. Enabled = true;
50 lbtNext. Enabled = true;
51 lblLast. Enabled = true;
52}
53 if (int. Parse (this. curPage. Text) = Pagelist)
54 {
55 lbtfirst. Enabled = true;
56 lbtPre. Enabled = true;
57 lbtNext. Enabled = false;
58 lblLast. Enabled = false;
59}
60}
61
62 # endregion
63
64 # region click Page events
65 // page 1
66 protected void lbtfirst_Click (object sender, EventArgs e)
67 {
68 this. curPage. Text = "1 ";
69 OnPageChange (sender, 1 );
70 state ();
71}
72 // Previous Page
73 protected void lbtPre_Click (object sender, EventArgs e)
74 {
75 this. curPage. Text = (int. Parse (this. curPage. Text)-1). ToString ();
76 OnPageChange (sender, int. Parse (this. curPage. Text ));
77 state ();
78}
79 // next page
80 protected void lbtNext_Click (object sender, EventArgs e)
81 {
82
83 this. curPage. Text = (int. Parse (this. curPage. Text) + 1). ToString ();
84 OnPageChange (sender, int. Parse (this. curPage. Text ));
85 state ();
86}
87 // last page
88 protected void lblLast_Click (object sender, EventArgs e)
89 {
90 this. curPage. Text = Pagelist. ToString ();
91 OnPageChange (sender, int. Parse (this. curPage. Text ));
92 state ();
93}
94 # endregion
95}
96}
You only need to trigger a paging event to obtain the dynamically changed current page number.
1 protected void Page_Load (object sender, EventArgs e)
2 {
3 showData (1 );
4 this. pager31.OnPageChange + = new controls. pageChangedHandler (pager31_PageIndexChanged );
5}
6 void pager31_PageIndexChanged (object sender, int nPageIndex)
7 {
8 showData (nPageIndex );
9}
10 www.2cto.com
11 private void showData (int nPageIndex)
12 {
13 pager31.PageCount = 20;
14 pager31.Pagelist = (new sqlHelper (). getAllPage ()/pager31.PageCount;
15 this. GridView1.DataSource = new sqlHelper (). getList (pager31.PageCount, nPageIndex-1 ,"");
16 this. GridView1.DataBind ();
17}
The code will continue to be downloaded in the next article. I am sorry to keep you waiting.
Author: meteor sword