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