分頁控件:
PageConten.ascx
1

<% @ Control Language = " C# " AutoEventWireup = " true " CodeBehind = " PageConten.ascx.cs " Inherits = " test_01.PageConten " %>
2

< br />
3

總共有 <%= row_count %> 頁
4

< br />
5

總共有 <%= rows %> 行記錄
6

<%= page_foot %>
7

< br />
8

目前是第 <%= pageIndex %> 頁
9

< input type = " hidden " id = " hidSub " name = " hidSub " value = " 0 " />
10

< script language = " javascript " type = " text/javascript " >
11

function selectPage()
12
{
13
var id = document.getElementById("sltPage").value;
14
submit(id);
15
}
16

function bindSlt(row_count)
17
{
18
for(var i=1;i<=row_count;i++)
19
{
20
document.getElementById("sltPage").options.add(new Option(i,i));
21
}
22
}
23

function submit(page)
24
{
25
document.getElementById("hidSub").value = page;
26
document.forms[0].submit();
27
}
28

</ script >
PageConten.ascx.cs
1

using System;
2

using System.Data;
3

using System.Configuration;
4

using System.Collections;
5

using System.Web;
6

using System.Web.Security;
7

using System.Web.UI;
8

using System.Web.UI.WebControls;
9

using System.Web.UI.WebControls.WebParts;
10

using System.Web.UI.HtmlControls;
11

using System.Text;
12

13

namespace test_01
14
{
15
public partial class PageConten : System.Web.UI.UserControl
16
{
17
public string page_foot;
18
public int row_count = 0;
19
public string pageIndex;
20
public int page_info; //每頁要顯示多少頁碼出來
21
public int rows; //總行數
22
public int page_rows; //每頁分多少行
23
public int isRequest; //是否帶url參數,1為帶,2為不帶
24
public string pageName; //頁面名稱,去掉.aspx的部分
25
26
protected void Page_Load(object sender, EventArgs e)
27
{
28
row_count = rows / page_rows;
29
int indexPage = 0; //目前頁
30
if (IsPostBack)
31
{
32
indexPage = int.Parse("0" + Request["hidSub"]);
33
if (indexPage == 0)
34
{
35
pageIndex = "1";
36
}
37
else
38
{
39
pageIndex = indexPage.ToString();
40
}
41
Session["index"] = indexPage.ToString();
42
PageInfo(indexPage);
43
}
44
else
45
{
46
if (isRequest == 1)
47
{
48
indexPage = int.Parse("0" + Request["page"]);
49
if (indexPage == 0)
50
{
51
pageIndex = "1";
52
}
53
else
54
{
55
pageIndex = indexPage.ToString();
56
}
57
PageRequestInfo(indexPage);
58
}
59
else
60
{
61
indexPage = int.Parse("0" + Request["hidSub"]);
62
if (indexPage == 0)
63
{
64
pageIndex = "1";
65
}
66
else
67
{
68
pageIndex = indexPage.ToString();
69
}
70
Session["index"] = indexPage.ToString();
71
PageInfo(indexPage);
72
}
73
}
74
}
75
76
/// <summary>
77
/// 分頁摸闆方法1,不帶url參數
78
/// </summary>
79
/// <param name="index_page">目前頁</param>
80
private void PageInfo(int index_page)
81
{
82
int back_page = index_page - 1; //上一頁
83
int next_page = index_page + 1; //下一頁
84
int last_page; //頁碼能顯示的最後一頁
85
int j;
86
int r = page_info / 2;
87
if (index_page == 0)
88
{
89
last_page = page_info;
90
j = 1;
91
}
92
else
93
{
94
if (index_page > r)
95
{
96
if (index_page > row_count - r)
97
{
98
last_page = row_count;
99
}
100
else
101
{
102
last_page = index_page + r;
103
}
104
}
105
else
106
{
107
last_page = page_info;
108
}
109
}
110
j = last_page - (page_info - 1);
111
StringBuilder foot = new StringBuilder();
112
foot.Append("<table><tr><td>");
113
foot.Append(" ");
114
if (index_page != 1 && index_page != 0)
115
{
116
foot.Append("<a href='javascript:submit(1);' style='text-decoration:none'>[首頁]</a>");
117
foot.Append("<a href='javascript:submit(" + back_page + ")' style='text-decoration:none'>[上一頁]</a>");
118
}
119
else
120
{
121
foot.Append("[首頁]");
122
foot.Append("[上一頁]");
123
}
124
foot.Append(" ");
125
if (row_count > page_info)
126
{
127
for (int i = j; i <= last_page; i++) //----------------------總頁數大于10的情況
128
{
129
if (i == 1 && index_page == 0)
130
{
131
foot.Append("[" + i + "]");
132
}
133
else
134
{
135
if (i == index_page)
136
{
137
foot.Append("[" + i + "]");
138
}
139
else
140
{
141
foot.Append("<a href='javascript:submit(" + i + ");' style='text-decoration:none'>[" + i + "]</a>");
142
}
143
}
144
foot.Append(" ");
145
}
146
}
147
else
148
{
149
for (int i = 1; i <= row_count; i++)
150
{
151
if (i == 1 && index_page == 0)
152
{
153
foot.Append("[" + i + "]");
154
}
155
else
156
{
157
if (i == index_page)
158
{
159
foot.Append("[" + i + "]");
160
}
161
else
162
{
163
foot.Append("<a href='javascript:submit(" + i + ");' style='text-decoration:none'>[" + i + "]</a>");
164
}
165
}
166
foot.Append(" ");
167
}
168
}
169
if (index_page != row_count)
170
{
171
if (index_page == 0)
172
{
173
foot.Append("<a href='javascript:submit(2);' style='text-decoration:none'>[下一頁]</a>");
174
}
175
else
176
{
177
foot.Append("<a href='javascript:submit(" + next_page + ");' style='text-decoration:none'>[下一頁]</a>");
178
}
179
foot.Append("<a href='javascript:submit(" + row_count + ");' style='text-decoration:none'>[尾頁]</a>");
180
}
181
else
182
{
183
foot.Append("[下一頁]");
184
foot.Append("[尾頁]");
185
}
186
foot.Append(" ");
187
foot.Append("轉到<select id='sltPage' οnchange='selectPage()'>");
188
for (int i = 1; i <= row_count; i++)
189
{
190
foot.Append("<option value=" + i + ">" + i + "</option>");
191
}
192
foot.Append("</select>");
193
foot.Append("</td>");
194
foot.Append("</tr></table>");
195
page_foot = foot.ToString();
196
}
197
198
/// <summary>
199
/// 分頁摸闆方法2,帶url參數
200
/// </summary>
201
/// <param name="indexPage">目前頁</param>
202
public void PageRequestInfo(int index_page)
203
{
204
int back_page = index_page - 1; //上一頁
205
int next_page = index_page + 1; //下一頁
206
int last_page; //頁碼能顯示的最後一頁
207
int j;
208
int r = page_info / 2;
209
if (index_page == 0)
210
{
211
last_page = page_info;
212
j = 1;
213
}
214
else
215
{
216
if (index_page > r)
217
{
218
if (index_page > row_count - r)
219
{
220
last_page = row_count;
221
}
222
else
223
{
224
last_page = index_page + r;
225
}
226
}
227
else
228
{
229
last_page = page_info;
230
}
231
}
232
j = last_page - (page_info - 1);
233
StringBuilder foot = new StringBuilder();
234
foot.Append("<table><tr><td>");
235
foot.Append(" ");
236
if (index_page != 1 && index_page != 0)
237
{
238
foot.Append("<a href='" + pageName + ".aspx?page=1' style='text-decoration:none'>[首頁]</a>");
239
foot.Append("<a href='" + pageName + ".aspx?page=" + back_page + "' style='text-decoration:none'>[上一頁]</a>");
240
}
241
else
242
{
243
foot.Append("[首頁]");
244
foot.Append("[上一頁]");
245
}
246
foot.Append(" ");
247
if (row_count > page_info)
248
{
249
for (int i = j; i <= last_page; i++) //----------------------總頁數大于10的情況
250
{
251
if (i == 1 && index_page == 0)
252
{
253
foot.Append("[" + i + "]");
254
}
255
else
256
{
257
if (i == index_page)
258
{
259
foot.Append("[" + i + "]");
260
}
261
else
262
{
263
foot.Append("<a href='" + pageName + ".aspx?page=" + i + "' style='text-decoration:none'>[" + i + "]</a>");
264
}
265
}
266
foot.Append(" ");
267
}
268
}
269
else
270
{
271
for (int i = 1; i <= row_count; i++)
272
{
273
if (i == 1 && index_page == 0)
274
{
275
foot.Append("[" + i + "]");
276
}
277
else
278
{
279
if (i == index_page)
280
{
281
foot.Append("[" + i + "]");
282
}
283
else
284
{
285
foot.Append("<a href='" + pageName + ".aspx?page=" + i + "' style='text-decoration:none'>[" + i + "]</a>");
286
}
287
}
288
foot.Append(" ");
289
}
290
}
291
if (index_page != row_count)
292
{
293
if (index_page == 0)
294
{
295
foot.Append("<a href='" + pageName + ".aspx?page=2' style='text-decoration:none'>[下一頁]</a>");
296
}
297
else
298
{
299
foot.Append("<a href='" + pageName + ".aspx?page=" + next_page + "' style='text-decoration:none'>[下一頁]</a>");
300
}
301
foot.Append("<a href='" + pageName + ".aspx?page=" + row_count + "' style='text-decoration:none'>[尾頁]</a>");
302
}
303
else
304
{
305
foot.Append("[下一頁]");
306
foot.Append("[尾頁]");
307
}
308
foot.Append(" ");
309
foot.Append("轉到<select id='sltPage' οnchange='selectPage()'>");
310
for (int i = 1; i <= row_count; i++)
311
{
312
foot.Append("<option value=" + i + ">" + i + "</option>");
313
}
314
foot.Append("</select>");
315
foot.Append("</td>");
316
foot.Append("</tr></table>");
317
page_foot = foot.ToString();
318
}
319
}
320
}
使用說明
在頁面的Page_Load方法體裡輸入以下屬性
PageConten1.rows = 368; //總行數
PageConten1.page_rows = 10; //每頁分多少行
PageConten1.page_info = 10; //每頁要顯示多少頁碼出來
PageConten1.isRequest = 1; //是否帶url參數,1為帶,2為不帶
if (PageConten1.isRequest == 1)
{
index_page = int.Parse(Request["page"] == null ? ("1" + Request["page"]) : Request["page"]);
PageConten1.pageName = "Test_4";
}
else
{
index_page = int.Parse(Request["hidSub"] == null ? ("1" + Request["hidSub"]) : Request["hidSub"]);
}
轉載于:https://www.cnblogs.com/Cien/archive/2009/03/24/1420394.html