温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:PersonalBlog源码
当前文件:
CommunityServerPersonalBlog/AppCode/Controls/Common/Pager.cs[9K,2009-6-12 11:37:08],打开代码结构图
CommunityServerPersonalBlog/AppCode/Controls/Common/Pager.cs[9K,2009-6-12 11:37:08],打开代码结构图1using System; 2
using System.Text; 3
using System.Text.RegularExpressions; 4
using System.IO; 5
using System.Web.UI; 6
using System.Web.UI.WebControls; 7
using System.Web.UI.HtmlControls; 8
using NetFocus.Web.Core; 9
10
namespace NetFocus.Web.Applications.PersonalBlog 11
...{ 12
public class Pager : Label, IPagedControl, INamingContainer 13
...{ 14
Member variables and constructor#region Member variables and constructor 15
16
private WebContext csContext = WebContext.Current; 17
private HyperLink previousLink; 18
private HyperLink nextLink; 19
private HyperLink firstLink; 20
private HyperLink lastLink; 21
private HyperLink[] pagingHyperLinks; 22
23
public override ControlCollection Controls 24
...{ 25
get 26
...{ 27
EnsureChildControls(); 28
return base.Controls; 29
} 30
} 31
protected override void CreateChildControls() 32
...{ 33
Controls.Clear(); 34
35
// Add Page buttons 36
// 37
AddPageLinks(); 38
39
// Add Previous Next child controls 40
// 41
AddPreviousNextLinks(); 42
43
// Add First Last child controls 44
// 45
AddFirstLastLinks(); 46
47
} 48
49
#endregion 50
51
Render functions#region Render functions 52
protected override void Render(HtmlTextWriter writer) 53
...{ 54
55
int totalPages = CalculateTotalPages(); 56
57
// Do we have data? 58
// 59
if (totalPages <= 1) 60
return; 61
62
// Render the paging buttons 63
// 64
writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false); 65
66
// Render the first button 67
// 68
RenderFirst(writer); 69
70
// Render the previous button 71
// 72
RenderPrevious(writer); 73
74
// Render the page button(s) 75
// 76
RenderPagingButtons(writer); 77
78
// Render the next button 79
// 80
RenderNext(writer); 81
82
// Render the last button 83
// 84
RenderLast(writer); 85
86
//writer.RenderEndTag(); 87
88
} 89
90
void RenderFirst (HtmlTextWriter writer) 91
...{ 92
93
int totalPages = CalculateTotalPages(); 94
95
if ((PageIndex >= 3) && (totalPages > 5)) 96
...{ 97
firstLink.RenderControl(writer); 98
99
LiteralControl l = new LiteralControl(" ... "); 100
l.RenderControl(writer); 101
} 102
} 103
104
void RenderLast (HtmlTextWriter writer) 105
...{ 106
int totalPages = CalculateTotalPages(); 107
108
if (((PageIndex + 3) < totalPages) && (totalPages > 5)) 109
...{ 110
LiteralControl l = new LiteralControl(" ... "); 111
l.RenderControl(writer); 112
113
lastLink.RenderControl(writer); 114
} 115
116
} 117
118
void RenderPrevious (HtmlTextWriter writer) 119
...{ 120
Literal l; 121
122
if (HasPrevious) 123
...{ 124
previousLink.RenderControl(writer); 125
126
l = new Literal(); 127
l.Text = " "; 128
l.RenderControl(writer); 129
} 130
131
} 132
133
void RenderNext(HtmlTextWriter writer) 134
...{ 135
Literal l; 136
137
if (HasNext) 138
...{ 139
140
l = new Literal(); 141
l.Text = " "; 142
l.RenderControl(writer); 143
144
nextLink.RenderControl(writer); 145
} 146
147
} 148
149
void RenderButtonRange(int start, int end, HtmlTextWriter writer) 150
...{ 151
152
for (int i = start; i < end; i++) 153
...{ 154
155
// Are we working with the selected index? 156
// 157
if (PageIndex == i) 158
...{ 159
160
// Render different output 161
// 162
Literal l = new Literal(); 163
//l.Text = "<span class=\"currentPage\">[" + (i + 1).ToString() + "]</span>"; 164
l.Text = (i + 1).ToString(); 165
166
l.RenderControl(writer); 167
} 168
else 169
...{ 170
pagingHyperLinks[i].RenderControl(writer); 171
} 172
if ( i < (end - 1) ) 173
writer.Write(" "); 174
175
} 176
177
} 178
179
void RenderPagingButtons(HtmlTextWriter writer) 180
...{ 181
int totalPages; 182
183
// Get the total pages available 184
// 185
totalPages = CalculateTotalPages(); 186
187
// If we have <= 5 pages display all the pages and exit 188
// 189
if ( totalPages <= 5) 190
...{ 191
RenderButtonRange(0, totalPages, writer); 192
} 193
else 194
...{ 195
196
int lowerBound = (PageIndex - 2); 197
int upperBound = (PageIndex + 3); 198
199
if (lowerBound <= 0) 200
lowerBound = 0; 201
202
if (PageIndex == 0) 203
RenderButtonRange(0, 5, writer); 204
205
else if (PageIndex == 1) 206
RenderButtonRange(0, (PageIndex + 4), writer); 207
208
else if (PageIndex < (totalPages - 2)) 209
RenderButtonRange(lowerBound, (PageIndex + 3), writer); 210
211
else if (PageIndex == (totalPages - 2)) 212
RenderButtonRange((totalPages - 5), (PageIndex + 2), writer); 213
214
else if (PageIndex == (totalPages - 1)) 215
RenderButtonRange((totalPages - 5), (PageIndex + 1), writer); 216
} 217
} 218
#endregion 219
220
ControlTree functions#region ControlTree functions 221
void AddPageLinks() 222
...{ 223
// First add the lower page buttons 224
// 225
pagingHyperLinks = new HyperLink[CalculateTotalPages()]; 226
227
// Create the buttons and add them to 228
// the Controls collection 229
// 230
for (int i = 0; i < pagingHyperLinks.Length; i++) 231
...{ 232
pagingHyperLinks[i] = new HyperLink(); 233
pagingHyperLinks[i].EnableViewState = false; 234
pagingHyperLinks[i].Text = (i + 1).ToString(); 235
pagingHyperLinks[i].ID = (i + 1).ToString(); 236
pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1).ToString()); 237
Controls.Add(pagingHyperLinks[i]); 238
} 239
} 240
241
void AddFirstLastLinks() 242
...{ 243
int start = 1; 244
firstLink = new HyperLink(); 245
firstLink.ID = "First"; 246
firstLink.Text = Resources.GetString("Utility_Pager_firstButton"); 247
firstLink.NavigateUrl = CreatePagerURL(start.ToString()); 248
Controls.Add(firstLink); 249
250
int last = CalculateTotalPages(); 251
lastLink = new HyperLink(); 252
lastLink.ID = "Last"; 253
lastLink.Text = Resources.GetString("Utility_Pager_lastButton"); 254
lastLink.NavigateUrl = CreatePagerURL(last.ToString()); 255
Controls.Add(lastLink); 256
} 257
258
void AddPreviousNextLinks() 259
...{ 260
int previous; 261
262
if (this.PageIndex < 2) 263
previous = 1; 264
else 265
previous = this.PageIndex; 266
267
previousLink = new HyperLink(); 268
previousLink.ID = "Prev"; 269
previousLink.Text = Resources.GetString("Utility_Pager_previousButton"); 270
previousLink.NavigateUrl = CreatePagerURL(previous.ToString()); 271
Controls.Add(previousLink); 272
273
int next = this.PageIndex + 2; 274
nextLink = new HyperLink(); 275
nextLink.ID = "Next"; 276
nextLink.Text = Resources.GetString("Utility_Pager_nextButton"); 277
nextLink.NavigateUrl = CreatePagerURL(next.ToString()); 278
Controls.Add(nextLink); 279
} 280
#endregion 281
282
Private Properties#region Private Properties 283
private bool HasPrevious 284
...{ 285
get 286
...{ 287
if (PageIndex > 0) 288
return true; 289
290
return false; 291
} 292
} 293
294
private bool HasNext 295
...{ 296
get 297
...{ 298
if (PageIndex + 1 < CalculateTotalPages() ) 299
return true; 300
301
return false; 302
} 303
} 304
#endregion 305
306
Helper methods and Public Properties#region Helper methods and Public Properties 307
protected virtual string CreatePagerURL(string pageIndex) 308
...{ 309
if(!string.IsNullOrEmpty(UrlPattern)) 310
return string.Format(UrlPattern,pageIndex); 311
312
if (!string.IsNullOrEmpty(UrlName)) 313
...{ 314
return UrlsData.Instance().FormatUrl(UrlName, new object[] ...{ pageIndex }); 315
} 316
317
if (csContext.CurrentUri.AbsoluteUri.IndexOf("?") == -1) 318
...{ 319
return csContext.CurrentUri.AbsoluteUri.ToString() + "?PageIndex=" + pageIndex; 320
} 321
else 322
...{ 323
if (csContext.CurrentUri.AbsoluteUri.IndexOf("PageIndex=") == -1) 324
return csContext.CurrentUri.AbsoluteUri.ToString() + "&PageIndex=" + pageIndex; 325
else 326
...{ 327
return Regex.Replace(csContext.CurrentUri.AbsoluteUri.ToString(), @"PageIndex=(\d+\.?\d*|\.\d+)", "PageIndex=" + pageIndex); 328
} 329
} 330
} 331
332
string _urlPattern = null; 333
public string UrlPattern 334
...{ 335
get ...{ return _urlPattern;} 336
set ...{_urlPattern = value;} 337
} 338
339
string _urlName = null; 340
public string UrlName 341
...{ 342
get ...{ return _urlName; } 343
set ...{ _urlName = value; } 344
} 345
346
347
public virtual int CalculateTotalPages() 348
...{ 349
int totalPagesAvailable; 350
351
if (TotalRecords == 0) 352
return 0; 353
354
// First calculate the division 355
// 356
totalPagesAvailable = TotalRecords / PageSize; 357
358
// Now do a mod for any remainder 359
// 360
if ((TotalRecords % PageSize) > 0) 361
totalPagesAvailable++; 362
363
return totalPagesAvailable; 364
365
} 366
367
368
int _pageIndex = 0; 369
public virtual int PageIndex 370
...{ 371
get 372
...{ 373
// Give first try to the ViewState if it was a postback 374
if (Page != null && Page.IsPostBack && ViewState["PageIndex"] != null)...{ 375
_pageIndex = (int) ViewState["PageIndex"]; 376
} 377
else ...{ 378
if (csContext.QueryString["pageindex"] != null) 379
_pageIndex = int.Parse(csContext.QueryString["pageindex"]) - 1; 380
} 381
382
if (_pageIndex < 0) 383
return 0; 384
else 385
return _pageIndex; 386
} 387
set 388
...{ 389
ViewState["PageIndex"] = value; 390
_pageIndex = value; 391
} 392
} 393
394
public virtual int PageSize 395
...{ 396
get 397
...{ 398
int pageSize = Convert.ToInt32(ViewState["PageSize"]); 399
400
if (pageSize == 0) 401
return 10; 402
403
return pageSize; 404
} 405
set 406
...{ 407
ViewState["PageSize"] = value; 408
} 409
410
} 411
412
private bool _causeValidation = true; 413
public bool CausesValidation 414
...{ 415
get ...{return _causeValidation;} 416
set ...{_causeValidation = value;} 417
} 418
419
public int TotalRecords 420
...{ 421
get 422
...{ 423
return Convert.ToInt32(ViewState["TotalRecords"]); 424
} 425
set 426
...{ 427
ViewState["TotalRecords"] = value; 428
} 429
} 430
431
public string UrlCleaner(string RawURL, string QueryName) 432
...{ 433
// Configure the Url 434
if(RawURL.IndexOf("?") != -1) 435
...{ 436
bool hasPart = false; 437
string queryString = RawURL.Substring( RawURL.IndexOf("?") + 1 ); 438
string[] parts = queryString.Split('&'); 439
for(int i = 0 ; i < parts.Length ; i++) 440
...{ 441
if(parts[i].StartsWith(QueryName)) 442
...{ 443
parts[i] = QueryName + "={0}"; 444
hasPart = true; 445
} 446
} 447
if(hasPart == true) 448
RawURL = RawURL.Replace(queryString, string.Join("&", parts)); 449
else 450
RawURL = RawURL.Replace(queryString, string.Join("&", parts) + "&" + QueryName + "={0}"); 451
} 452
else 453
RawURL = RawURL + "?" + QueryName + "={0}"; 454
455
return RawURL; 456
457
} 458
459
#endregion 460
461
} 462
}






}
}