分页代码
1
/* ***********************************************
2
* Copyright: ? 2001-2009
3
* Author: 作者:MILD
4
* Link: http://www.***.com
5
* Created Time: 2009-9-19 19:52:14
6
* Object Name: Mild.Core.PageListFactory
7
* FileName: PageListFactory
8
* Description: 该类的描述、功用、依赖性
9
* EditHistory: 修改记录
10
* ***********************************************/
11
12
/*
13
附上css样式
14
.pager{line-height:130%; margin:50px auto; width:80%; height:auto;}
15
.pager input{width:30px;border:solid 1px #dbdbdb;}
16
.pager a{padding:3px 5px; border:solid 1px #dbdbdb;}
17
.pager a:link,.pager a:visited{text-decoration:none;color:#666; margin-right:3px;}
18
.pager a.page-select{background:#000;color:#fff;font-weight:bold;}
19
*/
20
using System;
21
using System.Text;
22
using System.Web;
23
24
namespace Mild.Common.Core
25
{
26
/// <summary>
27
/// 类 PageListFactory 的说明
28
/// </summary>
29
public class PageListFactory
30
{
31
/// <summary>
32
/// 页码项模版
33
/// </summary>
34
public string NumItem = "<a href='{0}'>{1}</a>";
35
36
/// <summary>
37
/// 当前页码项模版
38
/// </summary>
39
public string CurrentNumItem = "<a class='page-select' href='javascript:void(0)'>{0}</a>";
40
41
/// <summary>
42
/// 盒子开始的标签
43
/// </summary>
44
public string BoxBeginTag = "<div class='pager'>";
45
46
/// <summary>
47
/// 盒子结束的标签
48
/// </summary>
49
public string BoxEndTag = "</div>";
50
51
/// <summary>
52
/// 记录总数
53
/// </summary>
54
public int Total = 1;
55
56
/// <summary>
57
/// 页大小
58
/// </summary>
59
public int PageSize = 1;
60
61
/// <summary>
62
/// 当前页
63
/// </summary>
64
public int CurrentPage = 1;
65
66
/// <summary>
67
/// 页数
68
/// </summary>
69
public int PageCount = 0;
70
71
/// <summary>
72
/// 当当前页大于该数字则显示更多
73
/// </summary>
74
public int MoreFlag = 4;
75
76
/// <summary>
77
/// 传递过来的当前页参数名称
78
/// </summary>
79
public string QueryStringName = "page";
80
81
/// <summary>
82
/// 更多的链接
83
/// </summary>
84
public string MoreLink = "<a href='{0}'>..</a>";
85
86
/// <summary>
87
/// URL 模版
88
/// </summary>
89
private string UrlWithoutPage = string.Empty;
90
91
/// <summary>
92
/// URL 地址上的参数模版
93
/// </summary>
94
private string UrlParameters = string.Empty;
95
96
/// <summary>
97
/// URL 重写的表达式
98
/// </summary>
99
private string UrlRewritePatten = string.Empty;
100
101
/// <summary>
102
/// 开始页码
103
/// </summary>
104
private int StartNum = 1;
105
106
/// <summary>
107
/// 结束页码
108
/// </summary>
109
private int EndNum = 0;
110
111
112
/// <summary>
113
/// 首页文本
114
/// </summary>
115
public string FirstPageText = "<a href='{0}'>首页</a>";
116
117
/// <summary>
118
/// 上一页文本
119
/// </summary>
120
public string PreviouPageText = "<a href='{0}'>上一页</a>";
121
122
/// <summary>
123
/// 下一页文本
124
/// </summary>
125
public string NextPageText = "<a href='{0}'>下一页</a>";
126
127
/// <summary>
128
/// 末页文本
129
/// </summary>
130
public string LastPageText = "<a href='{0}'>末页</a>";
131
132
/// <summary>
133
/// 更多页 - 向前
134
/// </summary>
135
public string MorePreviouText = "<a href='{0}'>..</a>";
136
137
/// <summary>
138
/// 更多页 - 向后
139
/// </summary>
140
public string MoreNextText = "<a href='{0}'>..</a>";
141
142
/// <summary>
143
/// 输入框
144
/// </summary>
145
public string InputToPageText = "<input type='text' onkeydown='_m_goto_page(event,this)' /><input type='button' value='转到' onclick='_m_goto_page(event,this)' /><script>function _m_goto_page(e,o){var ipts=o.parentNode.getElementsByTagName(\"input\");if(o.type=='text'){var kcode;if(window.event){kcode=e.keyCode}else if(e.which){kcode=e.which}if(kcode==13){if(document.all){ipts[1].click()}else if(document.createEvent){var ev=document.createEvent('HTMLEvents');ev.initEvent('click',false,true);ipts[1].dispatchEvent(ev)}}return}var page=ipts[0].value;var p=/\\d+/;if(!p.test(page)){alert(\"页码输入错误\")}else if(page>{maxpage}){alert(\"页码超过总页数,请重新输入\");ipts[0].value=\"\"}else{location.href='{url}'+ipts[0].value}}</script>";
146
147
private PageListFactory() { }
148
149
/// <summary>
150
/// 构造函数
151
/// </summary>
152
/// <param name="page"></param>
153
/// <param name="pagesize"></param>
154
/// <param name="total"></param>
155
/// <param name="ptn"></param>
156
private PageListFactory(int page, int pagesize, int total, string ptn)
157
{
158
this.Total = total;
159
this.CurrentPage = page;
160
this.PageSize = pagesize;
161
this.PageCount = (int)Math.Ceiling(Total * 1.0 / PageSize);
162
this.UrlRewritePatten = ptn;
163
164
if (page > PageCount)
165
{
166
CurrentPage = PageCount;
167
}
168
}
169
170
private void PrepareInterzone()
171
{
172
int flag = 0;
173
174
if (CurrentPage > MoreFlag)
175
{
176
flag = (int)Math.Ceiling(CurrentPage * 1.0 / MoreFlag);
177
178
if (flag >= 2)
179
{
180
StartNum = CurrentPage - MoreFlag;
181
182
MorePreviouText = string.Format(MorePreviouText, UrlWithoutPage + QueryStringName + "=" + (CurrentPage - MoreFlag));
183
}
184
else
185
{
186
MorePreviouText = "";
187
}
188
}
189
else
190
{
191
MorePreviouText = "";
192
}
193
194
int nflag = 0;
195
196
if (PageCount - CurrentPage > MoreFlag)
197
{
198
nflag = (int)Math.Ceiling((PageCount - CurrentPage) * 1.0 / MoreFlag);
199
200
if (nflag >= 2)
201
{
202
EndNum = CurrentPage + MoreFlag;
203
204
MoreNextText = string.Format(MoreNextText, UrlWithoutPage + QueryStringName + "=" + (CurrentPage + MoreFlag));
205
}
206
else
207
{
208
MoreNextText = "";
209
}
210
}
211
else
212
{
213
EndNum = PageCount;
214
215
MoreNextText = "";
216
}
217
}
218
219
/// <summary>
220
/// 准备所有的参数
221
/// </summary>
222
private void Prepare()
223
{
224
this.UrlWithoutPage = MakeUrl();
225
226
PrepareInterzone();
227
228
229
if (CurrentPage == 1)
230
{
231
FirstPageText = string.Format(FirstPageText, "javascript:;");
232
PreviouPageText = string.Format(PreviouPageText, "javascript:;");
233
}
234
else
235
{
236
FirstPageText = string.Format(FirstPageText, UrlWithoutPage + QueryStringName + "=1");
237
PreviouPageText = string.Format(PreviouPageText, UrlWithoutPage + QueryStringName + "=" + (CurrentPage - 1));
238
}
239
240
if (CurrentPage == PageCount)
241
{
242
LastPageText = string.Format(LastPageText, "javascript:;");
243
NextPageText = string.Format(NextPageText, "javascript:;");
244
}
245
else
246
{
247
LastPageText = string.Format(LastPageText, UrlWithoutPage + QueryStringName + "=" + PageCount);
248
NextPageText = string.Format(NextPageText, UrlWithoutPage + QueryStringName + "=" + (CurrentPage + 1));
249
}
250
}
251
252
/// <summary>
253
/// 获取当前页面的URL,但不包含页码参数
254
/// </summary>
255
/// <returns></returns>
256
private string MakeUrl()
257
{
258
HttpRequest Request = HttpContext.Current.Request;
259
string Url = Request.Url.AbsolutePath.Replace("/", "") + "?";
260
string[] queryNames=Request.QueryString.AllKeys;
261
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
262
263
string key = string.Empty;
264
string value = string.Empty;
265
for (int i = 0; i < queryNames.Length; i++)
266
{
267
key = queryNames[i];
268
value = Request.QueryString[key];
269
if (key == QueryStringName||value=="")
270
{
271
continue;
272
}
273
list.Add(key + "=" + value);
274
}
275
276
string paras = Url + string.Join("&", list.ToArray());
277
278
if (list.Count > 0)
279
{
280
paras += "&";
281
}
282
283
return paras;
284
}
285
286
public string CreateHtml()
287
{
288
Prepare();
289
290
StringBuilder sb = new StringBuilder();
291
sb.AppendLine(BoxBeginTag);
292
sb.AppendLine(FirstPageText);
293
sb.AppendLine(PreviouPageText);
294
sb.AppendLine(MorePreviouText);
295
for (int i = StartNum; i < EndNum+1; i++)
296
{
297
if (i == CurrentPage)
298
{
299
sb.AppendFormat(CurrentNumItem, i.ToString());
300
continue;
301
}
302
303
sb.AppendFormat(NumItem, UrlWithoutPage + QueryStringName + "=" + i, i.ToString());
304
}
305
sb.AppendLine(MoreNextText);
306
sb.AppendLine(NextPageText);
307
sb.AppendLine(LastPageText);
308
309
if (PageCount > 20)
310
{
311
sb.Append(InputToPageText.Replace("{maxpage}", PageCount.ToString()).Replace("{url}", UrlWithoutPage + "page="));
312
}
313
314
sb.AppendLine(BoxEndTag);
315
return sb.ToString();
316
}
317
318
/// <summary>
319
/// 获取分页html
320
/// </summary>
321
/// <param name="page">当前页</param>
322
/// <param name="pagesize">页大小(几条记录一页)</param>
323
/// <param name="total">总记录数</param>
324
/// <param name="ptn">未作,URL重写表达式</param>
325
/// <returns></returns>
326
public static string GetHtml(int page, int pagesize, int total, string ptn)
327
{
328
PageListFactory factory = new PageListFactory(page, pagesize, total, ptn);
329
330
return factory.CreateHtml();
331
}
332
333
/// <summary>
334
/// 获取分页html
335
/// </summary>
336
/// <param name="page">当前页</param>
337
/// <param name="pagesize">页大小(几条记录一页)</param>
338
/// <param name="total">总记录数</param>
339
/// <returns></returns>
340
public static string GetHtml(int page, int pagesize, int total)
341
{
342
return GetHtml(page, pagesize, total, "");
343
}
344
}
345
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix