The data binding control of asp.net has the built-in paging function, but the paging function is limited and the style is not easy to control. So I want to develop the paging control myself.
Don't laugh at the effect of first drying the picture
Display the front-end page paging code. The front-end uses linkbutton, and then triggers the event to change the page number. Bind the page number to the drop-down list control. You need to know the total page number. pagedatasource attribute PageCount
<Div id = "showTfenye">
Current page: <asp: Label ID = "lblPage" runat = "server" Text = "1"> </asp: Label> & nbsp; Total number of pages: <asp: Label
ID = "lblBackPage" runat = "server" Text = ""> </asp: Label> & nbsp;
<Asp: LinkButton ID = "lbtnOne" runat = "server" ForeColor = "Red" OnClick = "lbtnOne_Click"> homepage </asp: LinkButton> & nbsp;
<Asp: LinkButton ID = "lbtnUp" runat = "server" ForeColor = "Red" OnClick = "lbtnUp_Click"> previous page </asp: LinkButton> & nbsp;
<Asp: LinkButton ID = "lbtnNext" runat = "server" ForeColor = "Red" OnClick = "lbtnNext_Click"> next page </asp: LinkButton> & nbsp;
<Asp: LinkButton ID = "lbtnBack" runat = "server" ForeColor = "Red" OnClick = "lbtnBack_Click"> last page </asp: LinkButton> & nbsp;
<Asp: DropDownList ID = "DropDownList1" runat = "server" Width = "40px">
</Asp: DropDownList>
& Nbsp;
<Asp: Button ID = "Button1" runat = "server" BackColor = "#99 FFCC" BorderColor = "# 3333FF"
BorderWidth = "2px" OnClick = "button#click" Text = "Go"/>
</Div>
This page is encapsulated in the user control, using pagedatasource as the intermediate data source, and then transmitted to the data binding control, such as the GridView
Important use of pageDataSource
Ps. DataSource = dt. DefaultView;
// Whether paging is allowed
Ps. AllowPaging = true;
// Display the number of pages
Ps. PageSize = this. ShowCount;
// Obtain the page number of the current page
Ps. CurrentPageIndex = curPage-1;
// Display the number of pages
This. lblBackPage. Text = Convert. ToString (ps. PageCount );
1 using System;
2 using System. Data;
3 using System. Web. UI. WebControls;
4 namespace Pager. controls
5 {
6 public partial class pager1: System. Web. UI. UserControl
7 {
8 protected void Page_Load (object sender, EventArgs e)
9 {
10 if (! IsPostBack)
11 {
12 bind ();
13 showPage ();
14}
15}
16 # region defines related attributes
17
18 private DataBoundControl dataControl;
19 public DataBoundControl DataControl
20 {
21 get {return this. dataControl ;}
22 set {this. dataControl = value ;}
23}
24 # other region data source controls
25
26 // private BaseDataBoundControl dataControl; // data source control type
27 /// <summary>
28 // data binding control
29 /// </summary>
30 // public BaseDataBoundControl DataControl
31 //{
32 // get {return this. dataControl ;}
33 // set {this. dataControl = value ;}
34 //}
35 // DataList Control
36 // private DataList dataControl;
37 // public DataList DataControl
38 //{
39 // get {return this. dataControl ;}
40 // set {this. dataControl = value ;}
41 //}
42 // repeater control
43 // private Repeater dataControl;
44 // public Repeater DataControl
45 //{
46 // get {return this. dataControl ;}
47 // set {this. dataControl = value ;}
48 //}
49 # endregion
50 private int showCount; // Number of pages per page
51 // <summary>
52 // The number displayed on each page
53 /// </summary>
54 public int ShowCount
55 {
56 get {return this. showCount ;}
57 set {this. showCount = value ;}
58}
59 private DataTable objSource; // Data Source
60 /// <summary>
61 // data source of the DataTable type
62 // </summary>
63 public DataTable Objsource
64 {
65 get {return this. objSource ;}
66 set {this. objSource = value ;}
67}
68 # endregion
69
70 # region paging Method
71 # region Paging
72 /// <summary>
73 // Paging
74 /// </summary>
75 protected void bind ()
76 {
77 // obtain the current page number
78 int curPage = Convert. ToInt32 (this. lblPage. Text );
79 // use the PageDataSource class to implement pagination of Data Controls
80 PagedDataSource ps = new PagedDataSource ();
81 // obtain the data source
82 DataTable dt = new DataTable ();
83 dt = this. objSource;
84 ps. DataSource = dt. DefaultView;
85 // whether paging is allowed
86 ps. AllowPaging = true;
87 // display the number of pages
88 ps. PageSize = this. ShowCount;
89 // obtain the page number of the current page
90 ps. CurrentPageIndex = curPage-1;
91 this. lbtnUp. Enabled = true;
92 this. lbtnNext. Enabled = true;
93 this. lbtnBack. Enabled = true;
94 this. lbtnOne. Enabled = true;
95 if (curPage = 1)
96 {
97 // do not display the button on the first page
98 this. lbtnOne. Enabled = false;
99 // do not display the previous page button
100 this. lbtnUp. Enabled = false;
101}
102 if (curPage = ps. PageCount)
103 {
104 // the next page button is not displayed
105 this. lbtnNext. Enabled = false;
106 // do not display the button on the last page
107 this. lbtnBack. Enabled = false;
108}
109 // display the number of pages
110 this. lblBackPage. Text = Convert. ToString (ps. PageCount );
111 // bind to the data source control
112 this. DataControl. DataSource = ps;
113 this. DataControl. DataBind ();
114}
115 # endregion
116 # region page 1
117 /// <summary>
118 // page 1
119 /// </summary>
120 /// <param name = "sender"> </param>
121 /// <param name = "e"> </param>
122 protected void lbtnOne_Click (object sender, EventArgs e)
123 {
124 this. lblPage. Text = "1 ";
125 this. bind ();
126}
127 # endregion
128 # region Previous Page
129 /// <summary>
130 // Previous Page
131 /// </summary>
132 /// <param name = "sender"> </param>
133 /// <param name = "e"> </param>
134 protected void lbtnUp_Click (object sender, EventArgs e)
135 {
136 this. lblPage. Text = Convert. ToString (Convert. ToInt32 (this. lblPage. Text)-1 );
137 this. bind ();
138}
139 # endregion
140 # Next page of region
141 /// <summary>
142 // next page
143 /// </summary>
144 /// <param name = "sender"> </param>
145 /// <param name = "e"> </param>
146 protected void lbtnNext_Click (object sender, EventArgs e)
147 {
148 this. lblPage. Text = Convert. ToString (Convert. ToInt32 (this. lblPage. Text) + 1 );
149 this. bind ();
150}
151 # endregion
152 # last region page
153 /// <summary>
154 // The last page
155 /// </summary>
156 /// <param name = "sender"> </param>
157 /// <param name = "e"> </param>
158 protected void lbtnBack_Click (object sender, EventArgs e)
159 {
160 this. lblPage. Text = this. lblBackPage. Text;
161 this. bind ();
162}
163 # endregion
164 // display all pages
165 public void showPage ()
166 {
167 int count = Convert. ToInt32 (lblBackPage. Text );
168 int [] num = new int [count];
169 for (int I = 1; I <= count; I ++)
170 {
171 num [I-1] = I;
172 // DropDownList1.DataValueField = I. ToString (); // specify the value in the drop-down list
173}
174 DropDownList1.DataSource = num;
175 DropDownList1.DataBind ();
176}
177 // jump to the specified page
178 protected void button#click (object sender, EventArgs e)
179 {
180 string page = DropDownList1.SelectedValue;
181 lblPage. Text = page; // since the current page control is displayed
182 bind ();
183}
184 # endregion
185}
186}
The data source control DataControl is required for calling. The number of ShowCount entries is displayed on each page. The data source Objsource is of the DataTable type.
1 protected void Page_Load (object sender, EventArgs e)
2 {
3 // pagination of the gridview
4 this. pager11.DataControl = this. GridView1;
5 this. pager11.ShowCount = 20;
6 sqlHelper s = new sqlHelper ();
7 this. pager11.Objsource = s. getAll (). Tables [0];
8}
Note that the call Time Code cannot be written in Ispostback.
The Code is available on the next page.
Author: meteor sword