该控件成形如图:
主要功能如下:
用户设置每页行数 (或者不设置 则该控件默认每页10笔数据)
用户设置分页控件的数据源DataTable(或者输入查询sql)
该控件会先将传入的数据源保存到session(这样之后就不用重复查询)
然后根据用户的操作(第一页、上一页,下一页,最后页,Go页)
以及当前所在的页码
得到符合条件的返回数据 (即点击下一页或上一页后 需要正确显示的数据集)
同时 可以根据数据是否多于一页 来决定该控件是否需要显示
然后用户可以选用 这返回的DataTable数据集进行相关操作
(本控件 已加入部分js判断)
==============
示例源代码如下:
++++++++++++++++++
PageIndexCtl.ascx
+++++++++++++
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageIndexCtl.ascx.cs" Inherits="Controls_PageIndexCtl" %>
<table>
<tr id="tr_PageIndex" runat="server">
<td>
<asp:LinkButton ID="lbtn_FirstPage" runat="server" OnClick="lbtn_FirstPage_Click" >第一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_PrePage" runat="server" OnClick="lbtn_PrePage_Click" >上一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_NextPage" runat="server" OnClick="lbtn_NextPage_Click" >下一页</asp:LinkButton>
<asp:LinkButton ID="lbtn_LastPage" runat="server" OnClick="lbtn_LastPage_Click" >最后页</asp:LinkButton>
</td>
</tr>
<tr align="center" id="tr_GoPage" runat="server">
<td>
当前第
<asp:TextBox ID="txt_CurrPage" runat="server" Width="30px" Text="1"></asp:TextBox>
<asp:TextBox ID="txt_CurrPageReal" runat="server" Width="30px" Text="1" style="display:none;"></asp:TextBox>
页/共
<asp:TextBox ID="txt_TotalPage" runat="server" Width="30px" ReadOnly="True" Text="1"></asp:TextBox>
页
<asp:Button ID="btn_GoPage" runat="server" Text="GO" OnClick="btn_GoPage_Click"/></td>
</tr>
</table>
+++++++++++++++++++++
PageIndexCtl.ascx.cs
+++++++++++++++++++++
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12using Gentle.Common;
13using Gentle.Framework;
14using Gentle.Provider;
15
16
17
18/**//*
19 * 作者:Free
20 * 日期:2006/12/28
21 * 名称:系统所用的页面导航: 第一页 上一页 下一页 最后页 当前为第几页/共多少页
22 * 描述:
23 */
24
25public partial class Controls_PageIndexCtl : System.Web.UI.UserControl
26{
27
28 属性声明与公开#region 属性声明与公开
29
30 本控件所需要参数#region 本控件所需要参数
31
32 //私有声明
33 #region
34
35 private string selectSQL;
36
37 private bool isHavedData = true;
38
39 private int currPage = 1;
40 private int totalPage = 1;
41 private int countPerPage = 10;
42
43 private bool isAutoVisible = true;
44
45 private DataTable dt_result;
46
47 #endregion
48
49 //公开属性
50 #region
51
52 /**//// <summary>
53 /// 查询出数据的SQL语句 注意要有Order By
54 /// </summary>
55 [DataSysDescription("查询出数据的SQL语句 注意要有Order By")]
56 public string SelectSQL
57 {
58 get { return selectSQL; }
59 set { selectSQL = value; }
60 }
61
62 /**//// <summary>
63 /// 当前页码
64 /// </summary>
65 [DataSysDescription("当前页码")]
66 [DefaultSettingValue("1")]
67 public int CurrPage
68 {
69 get { return currPage; }
70 set { currPage = value;}
71 }
72
73 /**//// <summary>
74 /// 数据总行数/每页行数 = 总页数
75 /// </summary>
76 [DataSysDescription("数据总行数/每页行数 = 总页数")]
77 [DefaultSettingValue("1")]
78 public int TotalPage
79 {
80 get { return totalPage; }
81 set { totalPage = value; }
82 }
83
84 /**//// <summary>
85 /// 每页显示的数据行数
86 /// </summary>
87 [DataSysDescription("每页显示的数据行数")]
88 [DefaultSettingValue("2")]
89 public int CountPerPage
90 {
91 get
92 {
93 string perCountName = this.UniqueID + "CountPerPage";
94 if (ViewState[perCountName] != null)
95 {
96 return Convert.ToInt32(ViewState[perCountName].ToString());
97 }
98 else
99 {
100 return 10;//默认值
101 }
102 }
103 set
104 {
105 string perCountName = this.UniqueID + "CountPerPage";
106 if (ViewState[perCountName] != null)
107 {
108 ViewState[perCountName] = value;
109 }
110 else
111 {
112 ViewState.Add(perCountName, value);
113 }
114 }
115 }
116
117 [DataSysDescription("计算后 所得的DataTable")]
118 public DataTable dt_Result
119 {
120 get { return dt_result; }
121 set { dt_result = value; }
122 }
123
124
125 [DataSysDescription("是否是已经有了所有数据的DataTable")]
126 public bool IsHavedData
127 {
128 get { return isHavedData; }
129 set { isHavedData = value; }
130 }
131
132 [DataSysDescription("本次分页 需要用这个DataTable")]
133 public DataTable dt_UseThisResult
134 {
135 get
136 {
137 string dtName = this.UniqueID + "Result";
138 if (Session[dtName] != null)
139 {
140 return (DataTable)Session[dtName];
141 }
142 else
143 {
144 return null;
145 }
146 }
147 set
148 {
149 string dtName = this.UniqueID + "Result";
150 if (Session[dtName] != null)
151 {
152 Session[dtName] = value;
153 }
154 else
155 {
156 Session.Add(dtName, value);
157 }
158 }
159 }
160
161 [DataSysDescription("本控件是否总是显示 还是数据行数不够一页时 不用显示")]
162 public bool IsAutoVisible
163 {
164 get { return isAutoVisible; }
165 set { isAutoVisible = value; }
166 }
167 #endregion
168
169 #endregion
170
171 LinkButton 的 文字显示#region LinkButton 的 文字显示
172 #endregion
173
174 #endregion
175
176 事件声明与公开#region 事件声明与公开
177
178 public event EventHandler EventFirstPage;
179 public event EventHandler EventPrePage;
180 public event EventHandler EventNextPage;
181 public event EventHandler EventLastPage;
182 public event EventHandler EventGoPage;
183
184 #endregion
185
186 相关功能实现的方法#region 相关功能实现的方法
187
188 Page_Load#region Page_Load
189 protected void Page_Load(object sender, EventArgs e)
190 {
191
192 }
193 #endregion
194
195 第一页#region 第一页
196 protected void lbtn_FirstPage_Click(object sender, EventArgs e)
197 {
198 if (BindResult("firstPage"))
199 {
200 //触发用户所写事件
201 if (EventFirstPage != null)
202 EventFirstPage(this, e);
203 }
204
205 }
206 #endregion
207
208 上一页#region 上一页
209 protected void lbtn_PrePage_Click(object sender, EventArgs e)
210 {
211 if (BindResult("prePage"))
212 {
213 //触发用户所写事件
214 if (EventFirstPage != null)
215 EventFirstPage(this, e);
216 }
217 }
218 #endregion
219
220 后一页#region 后一页
221 protected void lbtn_NextPage_Click(object sender, EventArgs e)
222 {
223 if (BindResult("nextPage"))
224 {
225 //触发用户所写事件
226 if (EventFirstPage != null)
227 EventFirstPage(this, e);
228 }
229 }
230 #endregion
231
232 最后页#region 最后页
233 protected void lbtn_LastPage_Click(object sender, EventArgs e)
234 {
235 if (BindResult("lastPage"))
236 {
237 //触发用户所写事件
238 if (EventFirstPage != null)
239 EventFirstPage(this, e);
240 }
241
242 }
243 #endregion
244
245 GO到第几页#region GO到第几页
246 protected void btn_GoPage_Click(object sender, EventArgs e)
247 {
248 if (BindResult("goPage"))
249 {
250 //触发用户所写事件
251 if (EventFirstPage != null)
252 EventFirstPage(this, e);
253 }
254 }
255 #endregion
256
257 公共调用方法#region 公共调用方法
258 private bool BindResult(string strType)
259 {
260 try
261 {
262 regJS();
263
264 //int currPageShow = Convert.ToInt32(this.txt_CurrPage.Text.Trim());//页面显示出的当前页 可能会被用户改动过
265 int currTotalShow = Convert.ToInt32(this.txt_TotalPage.Text.Trim());
266
267 int realCurrPage = Convert.ToInt32(this.txt_CurrPageReal.Text.Trim());//目前数据的真实当前页
268
269 判断 当前页 总页 将到页 看 是否有必要继续#region 判断 当前页 总页 将到页 看 是否有必要继续
270 if (strType.Equals("firstPage"))
271 {
272 if (realCurrPage <= 1)
273 {
274 return false;
275 }
276 }
277
278 if (strType.Equals("prePage"))
279 {
280 if (realCurrPage <= 1)
281 {
282 return false;
283 }
284 }
285
286 if (strType.Equals("nextPage"))
287 {
288 if (realCurrPage >= currTotalShow)
289 {
290 return false;
291 }
292 }
293
294 if (strType.Equals("lastPage"))
295 {
296 if (realCurrPage >= currTotalShow)
297 {
298 return false;
299 }
300 }
301
302 if (strType.Equals("goPage"))
303 {
304 //int currPageShow = Convert.ToInt32(this.txt_CurrPage.Text.Trim());//页面显示出的当前页 可能会被用户改动过
305
306 //if (currPageShow < 1)
307 //{
308 // //return false;
309 // this.txt_CurrPage.Text = "1";
310 //}
311
312 //if (currPageShow > currTotalShow)
313 //{
314 // //return false;
315 // this.txt_CurrPage.Text =
316 //}
317 }
318 #endregion
319
320 DataTable dt = new DataTable();
321
322 判断 数据来源#region 判断 数据来源
323 if (isHavedData) //如果已经有了数据源 则 直接用
324 {
325 dt = this.dt_UseThisResult;
326 }
327 else
328 {
329 //根据用户所提供的SQL语句 得到 相对应的DataTable
330 string strCommand = this.SelectSQL;
331 SqlResult sr = Broker.Execute(strCommand);
332 dt = ObjectView.GetDataView(sr).Table;
333
334 this.dt_UseThisResult = dt;//加入Session
335
336 }
337 #endregion
338
339 int currRowCount = dt.Rows.Count;
340
341 if (currRowCount <= 0) //如果查无数据 直接返回
342 return false;
343 this.countPerPage = this.CountPerPage;//取得目前 每页行数
344
345 int totalPageShow = (currRowCount % this.countPerPage > 0) ? ((currRowCount / this.countPerPage) + 1) : (currRowCount / this.countPerPage);
346 //总页数
347 this.txt_TotalPage.Text = totalPageShow.ToString();
348 this.totalPage = totalPageShow;
349
350 int start = 0;
351 int end = 0;
352
353 根据情况 进行 起止点 计算#region 根据情况 进行 起止点 计算
354
355 if (strType.Equals("firstPage"))
356 {
357 //
358 start = 1;
359 end = currRowCount > this.countPerPage ? this.countPerPage : currRowCount;
360 //当前页数 显示
361 this.txt_CurrPage.Text = "1";
362 this.txt_CurrPageReal.Text = "1";
363
364 }
365
366 if (strType.Equals("prePage"))
367 {
368 end = (realCurrPage - 1) * this.countPerPage;
369 start = end - this.countPerPage + 1;
370 //当前页数 显示
371 this.txt_CurrPage.Text = Convert.ToString(realCurrPage - 1);
372 this.txt_CurrPageReal.Text = Convert.ToString(realCurrPage - 1);
373 }
374
375 if (strType.Equals("nextPage"))
376 {
377 start = realCurrPage * this.countPerPage + 1;
378 end = start + this.countPerPage - 1;
379 //当前页数 显示
380 this.txt_CurrPage.Text = Convert.ToString((realCurrPage + 1));
381 this.txt_CurrPageReal.Text = Convert.ToString((realCurrPage + 1));
382 }
383
384 if (strType.Equals("lastPage"))
385 {
386 start = (totalPageShow - 1) * this.countPerPage + 1;
387 end = currRowCount;
388 //当前页数 显示
389 this.txt_CurrPage.Text = totalPageShow.ToString();
390 this.txt_CurrPageReal.Text = totalPageShow.ToString();
391 }
392
393 if (strType.Equals("goPage"))
394 {
395 int currPageShow = Convert.ToInt32(this.txt_CurrPage.Text.Trim());//页面显示出的当前页 可能会被用户改动过
396 if (currPageShow < 1)
397 currPageShow = 1;
398 if (currPageShow > totalPageShow)
399 currPageShow = totalPageShow;
400
401 start = (currPageShow - 1) * this.countPerPage + 1;
402 end = (start + this.countPerPage - 1) > currRowCount ? currRowCount : (start + this.countPerPage - 1);
403 //当前页数 显示
404 this.txt_CurrPage.Text = currPageShow.ToString();
405 this.txt_CurrPageReal.Text = currPageShow.ToString();
406 }
407
408 this.currPage = Convert.ToInt32(this.txt_CurrPageReal.Text.Trim());
409
410 #endregion
411
412 进行指定数据的转移#region 进行指定数据的转移
413 DataTable tmpdt = dt.Copy();
414 tmpdt.Clear();
415
416 int tmpCount = 0;
417 tmpCount = end > currRowCount ? currRowCount : end;
418 for (int i = 0; i < tmpCount; i++)
419 {
420 if ((i >= (start - 1)) && (i < end))
421 {
422 DataRow tmpdr = tmpdt.NewRow();
423 for (int j = 0; j < dt.Columns.Count; j++)
424 {
425 tmpdr[j] = dt.Rows[i][j];
426 }
427 tmpdt.Rows.Add(tmpdr);
428 }
429 }
430
431 //用户 可以用dt_Result来将数据绑定自己需要的控件上
432 this.dt_result = tmpdt;
433
434 #endregion
435
436 return true;
437
438 }
439 catch (Exception ex)
440 {
441 ex.ToString();
442 return false;
443 }
444 }
445 #endregion
446
447 根据用户提供的数据源 自己计算总页数 和得到第一页资料#region 根据用户提供的数据源 自己计算总页数 和得到第一页资料
448 /**//// <summary>
449 /// 根据用户提供的数据源 自己计算总页数 和得到第一页资料
450 /// </summary>
451 /// <param name="dtSources">用户提供的数据源</param>
452 /// <returns>得到第一页资料</returns>
453 public DataTable selfMeasure(DataTable dtSources)
454 {
455 DataTable dt = dtSources;
456
457 int currRowCount = dt.Rows.Count;
458
459 //将指定的DataTable作为数据源选项打开
460 this.dt_UseThisResult = dt;
461
462 if (currRowCount <= 0)//如果查无数据 直接返回
463 return null;
464
465 this.countPerPage = this.CountPerPage;//取得目前 每页行数
466
467 int totalPageShow = (currRowCount % this.countPerPage > 0) ? ((currRowCount / this.countPerPage) + 1) : (currRowCount / this.countPerPage);
468 //总页数
469 this.txt_TotalPage.Text = totalPageShow.ToString();
470 this.totalPage = totalPageShow;
471
472 int start = 0;
473 int end = 0;
474
475 if (currPage > totalPageShow)
476 {
477 this.currPage = totalPageShow;
478 this.txt_CurrPage.Text = totalPageShow.ToString();
479 //最后页
480 start = (totalPageShow - 1) * this.countPerPage + 1;
481 end = currRowCount;
482 }
483 else
484 {
485 this.txt_CurrPage.Text = this.currPage.ToString();
486 //指定页 默认的为第一页
487 start = (Convert.ToInt32(this.txt_CurrPage.Text.Trim()) - 1) * this.countPerPage + 1;
488 end = (start + this.countPerPage - 1) > currRowCount ? currRowCount : (start + this.countPerPage - 1);
489 }
490
491 进行指定数据的转移#region 进行指定数据的转移
492 DataTable tmpdt = dt.Copy();
493 tmpdt.Clear();
494
495 int tmpCount = 0;
496 tmpCount = end > currRowCount ? currRowCount : end;
497 for (int i = 0; i < tmpCount; i++)
498 {
499 if ((i >= (start - 1)) && (i < end))
500 {
501 DataRow tmpdr = tmpdt.NewRow();
502 for (int j = 0; j < dt.Columns.Count; j++)
503 {
504 tmpdr[j] = dt.Rows[i][j];
505 }
506 tmpdt.Rows.Add(tmpdr);
507 }
508 }
509
510 //用户 可以用dt_Result来将数据绑定自己需要的控件上
511 this.dt_result = tmpdt;
512
513 //根据所查询出的数据行数 决定是否需要显示本控件
514 if (isAutoVisible)
515 {
516 if (currRowCount <= this.countPerPage)
517 {
518 this.Visible = false;
519
520 }
521 else
522 {
523 this.Visible = true;
524 regJS();
525 }
526
527 }
528
529 #endregion
530
531 return tmpdt;
532
533 }
534 #endregion
535
536 注册JS#region 注册JS
537 private void regJS()
538 {
539 注册相关js事件#region 注册相关js事件
540 string strGuid = this.UniqueID;
541 string fnNameFirstPre = strGuid + "FirstPre()";
542 string fnNameNextLast = strGuid + "NextLast()";
543 string fnNameGo = strGuid + "Go()";
544
545 string txtCurrPageRealName = strGuid + "_txt_CurrPageReal";//数据真实所在的页
546 string txtCurrPageName = strGuid + "_txt_CurrPage";//页面显示的当前页
547 string txtTotalPageName = strGuid + "_txt_TotalPage";
548 string strJS = "<script language='javascript' type='text/javascript'>";
549 strJS += "var jsCurrPageReal = document.all." + txtCurrPageRealName + ".value;";
550 strJS += "var jsTotalPage = document.all." + txtTotalPageName + ".value;";
551 strJS += "var jsCurrPage = document.all." + txtCurrPageName + ".value;";
552 strJS += "function " + fnNameFirstPre + " ";
553 strJS += "{ if(jsCurrPageReal == 1) { ";
554 strJS += " document.all." + txtCurrPageName + ".value=1;";
555 strJS += " alert('已经是第一页'); ";
556 strJS += " return false; } }";
557
558 strJS += "function " + fnNameNextLast + " ";
559 strJS += "{ if(jsCurrPageReal == jsTotalPage) { ";
560 strJS += " document.all." + txtCurrPageName + ".value=jsTotalPage;";
561 strJS += " alert('已经是最后一页'); ";
562 strJS += " return false; } }";
563
564 //strJS += "function " + fnNameGo + " ";
565 //strJS += "{ if(jsCurrPage > jsTotalPage || jsCurrPage < 1) { ";
566 //strJS += " alert('请输入有效数值'); ";
567 //strJS += " return false; } }";
568
569 strJS += "</script>";
570
571 string jsName = strGuid + "pageJS";
572 if (!Page.IsStartupScriptRegistered(jsName))
573 {
574 Page.RegisterStartupScript(jsName, strJS);
575 }
576
577 this.lbtn_FirstPage.Attributes.Add("onclick", "return " + fnNameFirstPre + ";");
578 this.lbtn_PrePage.Attributes.Add("onclick", "return " + fnNameFirstPre + ";");
579 this.lbtn_NextPage.Attributes.Add("onclick", "return " + fnNameNextLast + ";");
580 this.lbtn_LastPage.Attributes.Add("onclick", "return " + fnNameNextLast + ";");
581 //this.btn_GoPage.Attributes.Add("onclick", "return " + fnNameGo + ";");
582
583 #endregion
584 }
585 #endregion
586
587 #endregion
588
589}
590
++++++++++++++++++++++++
testPageIndexCtl.aspx.cs
++++++++++++++++++++++++
{
string strComm = " SELECT * From Table1 order by id aesc ";
SqlResult sr = Broker.Execute(strComm);
DataTable dt = ObjectView.GetDataView(sr).Table;
this.PageIndexCtl1.CountPerPage = 5;
GridView1.DataSource = PageIndexCtl1.selfMeasure(dt);
GridView1.DataBind();
}
protected void Page_Init()
{
//订阅事件
this.PageIndexCtl1.EventFirstPage += new System.EventHandler(this.EventFirstPage);
this.PageIndexCtl1.EventPrePage += new System.EventHandler(this.EventPrePage);
this.PageIndexCtl1.EventNextPage += new System.EventHandler(this.EventNextPage);
this.PageIndexCtl1.EventLastPage += new System.EventHandler(this.EventLastPage);
this.PageIndexCtl1.EventGoPage += new System.EventHandler(this.EventGoPage);
}
private void EventFirstPage(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PageIndexCtl1.dt_Result;
this.GridView1.DataBind();
}
private void EventPrePage(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PageIndexCtl1.dt_Result;
this.GridView1.DataBind();
}
private void EventNextPage(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PageIndexCtl1.dt_Result;
this.GridView1.DataBind();
}
private void EventLastPage(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PageIndexCtl1.dt_Result;
this.GridView1.DataBind();
}
private void EventGoPage(object sender, EventArgs e)
{
this.GridView1.DataSource = this.PageIndexCtl1.dt_Result;
this.GridView1.DataBind();
}