近来一直在做Web开发,其中访问数据库,显示数据库的数据是最常做的事情.ASP.net 2.0为我们提供了GridView , DetailView ,Repeater ,FormView来显示数据,其中,Repeater是最小巧的,最灵活的一个.但与GridView等比起来,其中一个美中不足的地方就是没有分页显示功能.因此,最近几天一直在想办法,给Repeater添加分页功能.其中,试过用户控件,用户部件和自定义控件几种方法.个人觉得,最后一种方法扩展的控件,用起来最方便.因此,在此,贴出来.源代码如下:
Code
1
using System;
2
3
using System.Collections;
4
5
using System.Data;
6
7
using System.Configuration;
8
9
using System.Web;
10
11
using System.Web.Security;
12
13
using System.Web.UI;
14
15
using System.Web.UI.WebControls;
16
17
using System.Web.UI.WebControls.WebParts;
18
19
using System.Web.UI.HtmlControls;
20
21
using System.ComponentModel;
22
23
24
25
26
27
namespace Frontview.WebControls
28
29

{
30
31
32
33
/**//// <summary>
34
35
/// PagedRepeater 的摘要说明
36
37
/// </summary>
38
39
40
41
42
43
public class PagedRepeater : Repeater
44
45
{
46
47
48
49
private PagedDataSource m_pagedDataSource;
50
51
52
53
/**//// <summary>
54
55
/// 当前页
56
57
/// </summary>
58
59
[
60
61
DefaultValue("0"),
62
63
Bindable(false),
64
65
Category("行为"),
66
67
Localizable(true),
68
69
Description("当前页")
70
71
]
72
73
public int PageIndex
74
75
{
76
77
get
78
79
{
80
81
return this.m_pagedDataSource.CurrentPageIndex;
82
83
}
84
85
set
86
87
{
88
89
this.m_pagedDataSource.CurrentPageIndex = value;
90
91
}
92
93
}
94
95
96
97
/**//// <summary>
98
99
/// 每页项数,默认为10
100
101
/// </summary>
102
103
[
104
105
DefaultValue("10"),
106
107
Bindable(false),
108
109
Category("行为"),
110
111
Localizable(true),
112
113
Description("每页项数")
114
115
]
116
117
public int PageSize
118
119
{
120
121
get
122
123
{
124
125
return m_pagedDataSource.PageSize;
126
127
}
128
129
set
130
131
{
132
133
this.m_pagedDataSource.PageSize = value;
134
135
this.DataBind();
136
137
}
138
139
}
140
141
142
143
144
145
146
147
[
148
149
Description("总页数")
150
151
]
152
153
public int PageCount
154
155
{
156
157
get
158
159
{
160
161
return m_pagedDataSource.PageCount;
162
163
}
164
165
}
166
167
168
169
public PagedRepeater()
170
171
{
172
173
this.m_pagedDataSource = new PagedDataSource();
174
175
this.m_pagedDataSource.AllowPaging = true;
176
177
178
179
}
180
181
182
183
protected override void OnInit(EventArgs e)
184
185
{
186
187
188
189
base.OnInit(e);
190
191
this.m_pagedDataSource.DataSource = this.GetData();
192
193
this.DataSourceID = "";
194
195
this.DataSource = this.m_pagedDataSource;
196
197
this.DataBind();
198
199
}
200
201
}
202
203
}
204
205
其中,还有一点,不太令我自己满意的地方就是,不能用代码动态改变,扩展后的Repeater控件的DataSourceID和DataSource属性.