简单地扩展GridView控件的功能。扩展的功能有:当前页/共几页,首页 上一页 下一页 末页 go[]ok
而且还能把gridview导入到excel中。

Code
1
namespace KFC
2

{
3
using System;
4
using System.ComponentModel;
5
using System.Drawing.Design;
6
using System.IO;
7
using System.Runtime.InteropServices;
8
using System.Web;
9
using System.Web.UI;
10
using System.Web.UI.HtmlControls;
11
using System.Web.UI.WebControls;
12
13
public class myGridView : GridView
14
{
15
protected void btnExport_Click(object sender, ImageClickEventArgs e)
16
{
17
ImageButton button = (ImageButton)sender;
18
GridView parent = (GridView)button.Parent.Parent.Parent.Parent;
19
HttpResponse response = this.Context.ApplicationInstance.Response;
20
if ((parent.Rows.Count + 1) < 0x10000)
21
{
22
parent.AllowPaging = false;
23
parent.DataBind();
24
StringWriter writer = new StringWriter();
25
HtmlTextWriter writer2 = new HtmlTextWriter(writer);
26
HtmlForm child = new HtmlForm();
27
response.Charset = "gb2312";
28
response.ContentEncoding = System.Text.Encoding.UTF8;
29
response.ContentType = "application/vnd.ms-word";
30
response.AddHeader("content-disposition", "attachment;filename=Report.doc");
31
32
33
this.EnableViewState = false;
34
this.Page.Controls.Add(child);
35
child.Controls.Add(parent);
36
this.ClearControls(parent);
37
child.RenderControl(writer2);
38
response.Write(writer.ToString());
39
response.End();
40
parent.AllowPaging = true;
41
parent.DataBind();
42
}
43
else
44
{
45
response.Write("记录数量过大,无法导出到Excel。");
46
}
47
}
48
49
protected void Button1_Click(object sender, EventArgs e)
50
{
51
Button button = (Button)sender;
52
myGridView parent = (myGridView)button.Parent.Parent.Parent.Parent;
53
HttpResponse response = this.Context.ApplicationInstance.Response;
54
response.Clear();
55
response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
56
response.Charset = "gb2312";
57
response.ContentType = "application/vnd.xls";
58
StringWriter writer = new StringWriter();
59
HtmlTextWriter writer2 = new HtmlTextWriter(writer);
60
parent.AllowPaging = false;
61
parent.DataBind();
62
parent.RenderControl(writer2);
63
response.Write(writer.ToString());
64
response.End();
65
parent.AllowPaging = true;
66
parent.DataBind();
67
}
68
69
private void ClearControls(Control control)
70
{
71
for (int i = control.Controls.Count - 1; i >= 0; i--)
72
{
73
this.ClearControls(control.Controls[i]);
74
}
75
if ((control is ImageButton) || (control is Image))
76
{
77
control.Parent.Controls.Remove(control);
78
}
79
if (control is TableCell)
80
{
81
for (int j = 0; j < control.Controls.Count; j++)
82
{
83
LiteralControl control2;
84
if ((control.Controls[j] is ImageButton) || (control.Controls[j] is Image))
85
{
86
control.Controls.Remove(control.Controls[j]);
87
}
88
if (control.Controls[j] is CheckBox)
89
{
90
CheckBox box = (CheckBox) control.Controls[j];
91
control2 = new LiteralControl();
92
control2.Text = box.Checked ? "true" : "false";
93
control.Controls.Add(control2);
94
control.Controls.Remove(box);
95
}
96
else if (!(control.Controls[j] is Label) && !(control.Controls[j] is LiteralControl))
97
{
98
Control control3 = control.Controls[j];
99
if (control3.GetType().GetProperty("SelectedItem") != null)
100
{
101
control2 = new LiteralControl();
102
try
103
{
104
control2.Text = control3.GetType().GetProperty("SelectedItem").GetValue(control3, null).ToString();
105
}
106
catch (Exception)
107
{
108
}
109
control.Controls.Add(control2);
110
control.Controls.Remove(control3);
111
}
112
else if (control3.GetType().GetProperty("Text") != null)
113
{
114
control2 = new LiteralControl();
115
control2.Text = control3.GetType().GetProperty("Text").GetValue(control3, null).ToString();
116
control.Controls.Add(control2);
117
control.Controls.Remove(control3);
118
}
119
}
120
}
121
}
122
}
123
124
private void CustomizePageBar(GridViewRowEventArgs e)
125
{
126
127
Label label = new Label();
128
label.Text = (this.PageIndex + 1) + "/" + this.PageCount;
129
label.Font.Bold = true;
130
131
TableCell cell2 = new TableCell();
132
cell2.Width = Unit.Pixel(80);
133
cell2.Controls.Add(label);
134
135
/**//*
136
Label lbTotal = new Label();
137
lbTotal.Font.Bold = true;
138
int currentPageIndex = this.PageIndex;
139
this.PageIndex = this.PageCount - 1;
140
141
lbTotal.Text = "共"+(this.PageCount - 1) * this.PageSize + this.Rows.Count+"条记录";
142
143
this.PageIndex = currentPageIndex;
144
145
TableCell cellTotal = new TableCell();
146
cellTotal.Width = Unit.Pixel(120);
147
cellTotal.Controls.Add(lbTotal);
148
*/
149
150
151
152
LinkButton lbtnFirst = new LinkButton();
153
lbtnFirst.ID = "lbtnfirst";
154
lbtnFirst.Text = "首 页";
155
lbtnFirst.Font.Bold = true;
156
lbtnFirst.Font.Size = base.PagerStyle.Font.Size;
157
lbtnFirst.Click += new EventHandler(this.Pagination);
158
lbtnFirst.CommandArgument = "First";
159
160
TableCell cellFirst = new TableCell();
161
cellFirst.Width = Unit.Pixel(70);
162
cellFirst.Controls.Add(lbtnFirst);
163
164
LinkButton lbtnPrev = new LinkButton();
165
lbtnPrev.ID = "lbtnprev";
166
lbtnPrev.Text = "上一页";
167
lbtnPrev.Font.Bold = true;
168
lbtnPrev.Font.Size = base.PagerStyle.Font.Size;
169
lbtnPrev.Click+=new EventHandler(this.Pagination);
170
lbtnPrev.CommandArgument = "Prev";
171
172
TableCell cellPrev = new TableCell();
173
cellPrev.Width = Unit.Pixel(70);
174
cellPrev.Controls.Add(lbtnPrev);
175
176
LinkButton lbtnNext = new LinkButton();
177
lbtnNext.ID = "lbtnnext";
178
lbtnNext.Text = "下一页";
179
lbtnNext.Font.Bold = true;
180
lbtnNext.Font.Size = base.PagerStyle.Font.Size;
181
lbtnNext.Click+=new EventHandler(this.Pagination);
182
lbtnNext.CommandArgument = "Next";
183
184
TableCell cellNext = new TableCell();
185
cellNext.Width = Unit.Pixel(70);
186
cellNext.Controls.Add(lbtnNext);
187
188
LinkButton lbtnLast = new LinkButton();
189
lbtnLast.ID = "lbtnLast";
190
lbtnLast.Text = "末 页";
191
lbtnLast.Font.Bold = true;
192
lbtnLast.Font.Size = base.PagerStyle.Font.Size;
193
lbtnLast.Click+=new EventHandler(this.Pagination);
194
lbtnLast.CommandArgument = "Last";
195
196
TableCell cellLast = new TableCell();
197
cellLast.Width = Unit.Pixel(70);
198
cellLast.Controls.Add(lbtnLast);
199
200
//cellFirst.Controls.Add(lbtnFirst);
201
//cellFirst.Controls.Add(lbtnPrev);
202
//cellFirst.Controls.Add(lbtnNext);
203
//cellFirst.Controls.Add(lbtnLast);
204
205
206
207
Label label2 = new Label();
208
label2.Text = "go: ";
209
label2.ID = "lblGoTo";
210
label2.Font.Bold = true;
211
label2.Font.Size = base.PagerStyle.Font.Size;
212
213
TableCell cell3 = new TableCell();
214
//cell3.Width = Unit.Pixel(0x10);
215
216
cell3.Controls.Add(label2);
217
218
219
DropDownList list = new DropDownList();
220
list.ID = "ddlPick";
221
list.AutoPostBack = true;
222
list.EnableViewState = true;
223
list.Font.Size = base.PagerStyle.Font.Size;
224
225
for (int i = 1; i <= this.PageCount; i++)
226
{
227
list.Items.Add(i.ToString());
228
}
229
list.SelectedIndex = this.PageIndex;
230
list.SelectedIndexChanged += new EventHandler(this.OnPagePicked);
231
232
TextBox txtPage = new TextBox();
233
txtPage.Text = "";
234
txtPage.Width = Unit.Pixel(20);
235
txtPage.EnableViewState = true;
236
txtPage.Font.Size = base.PagerStyle.Font.Size;
237
238
239
TableCell cell4 = new TableCell();
240
//cell4.Width = Unit.Pixel(0x10);
241
242
243
Button btnOK = new Button();
244
btnOK.ID = "btnok";
245
btnOK.Text = "OK";
246
btnOK.Click+=new EventHandler(this.OnbtnOK_Click);
247
248
cell4.Controls.Add(txtPage);
249
cell4.Controls.Add(btnOK);
250
251
252
TableCell cell5 = new TableCell();
253
cell5.Width = Unit.Pixel(150);
254
255
foreach (Control control in e.Row.Cells[0].Controls)
256
{
257
cell5.Controls.Add(control);
258
}
259
260
Table child = new Table();//
261
child.BorderWidth = 0;
262
child.CellPadding = 0;
263
child.CellSpacing = 0;
264
child.Width = Unit.Percentage(100.0);
265
child.Height = Unit.Pixel(20);
266
267
child.Rows.Add(new TableRow());
268
269
child.Rows[0].Cells.Add(cell2);
270
271
//child.Rows[0].Cells.Add(cellTotal);
272
child.Rows[0].Cells.Add(cellFirst);
273
child.Rows[0].Cells.Add(cellPrev);
274
child.Rows[0].Cells.Add(cellNext);
275
child.Rows[0].Cells.Add(cellLast);
276
277
child.Rows[0].Cells.Add(cell5);
278
child.Rows[0].Cells.Add(cell3);
279
child.Rows[0].Cells.Add(cell4);
280
e.Row.Cells[0].Controls.Add(child);
281
}
282
283
protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
284
{
285
string[] sortColumns = sortExpression.Split(",".ToCharArray());
286
for (int i = 0; i < dgItem.Cells.Count; i++)
287
{
288
if ((dgItem.Cells[i].Controls.Count > 0) && (dgItem.Cells[i].Controls[0] is LinkButton))
289
{
290
string str;
291
int num2;
292
string commandArgument = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
293
this.SearchSortExpression(sortColumns, commandArgument, out str, out num2);
294
if (num2 > 0)
295
{
296
string str3 = str.Equals("ASC") ? this.SortAscImageUrl : this.SortDescImageUrl;
297
if (str3 != string.Empty)
298
{
299
Image child = new Image();
300
child.ImageUrl = str3;
301
dgItem.Cells[i].Controls.Add(child);
302
Label label = new Label();
303
label.Font.Size = FontUnit.Small;
304
label.Text = num2.ToString();
305
dgItem.Cells[i].Controls.Add(label);
306
}
307
else
308
{
309
Label label2 = new Label();
310
label2.Font.Size = FontUnit.XSmall;
311
label2.Font.Name = "webdings";
312
label2.EnableTheming = false;
313
label2.Text = str.Equals("ASC") ? "5" : "6";
314
dgItem.Cells[i].Controls.Add(label2);
315
if (this.AllowMultiColumnSorting)
316
{
317
Literal literal = new Literal();
318
literal.Text = num2.ToString();
319
dgItem.Cells[i].Controls.Add(literal);
320
}
321
}
322
}
323
}
324
}
325
}
326
327
protected string GetSortExpression(GridViewSortEventArgs e)
328
{
329
string[] sortColumns = null;
330
string sortExpression = this.SortExpression;
331
if (sortExpression != string.Empty)
332
{
333
sortColumns = sortExpression.Split(",".ToCharArray());
334
}
335
if ((sortExpression.IndexOf(e.SortExpression) > 0) || sortExpression.StartsWith(e.SortExpression))
336
{
337
sortExpression = this.ModifySortExpression(sortColumns, e.SortExpression);
338
}
339
else
340
{
341
sortExpression = sortExpression + ("," + e.SortExpression + " ASC ");
342
}
343
return sortExpression.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
344
}
345
346
protected void imgToggle_Click(object sender, ImageClickEventArgs e)
347
{
348
ImageButton button = (ImageButton)sender;
349
GridView parent = (GridView)button.Parent.Parent.Parent.Parent;
350
351
parent.AllowPaging = !parent.AllowPaging;
352
}
353
354
protected string ModifySortExpression(string[] sortColumns, string sortExpression)
355
{
356
string str = sortExpression + " ASC ";
357
string str2 = sortExpression + " DESC ";
358
for (int i = 0; i < sortColumns.Length; i++)
359
{
360
if (str.Equals(sortColumns[i]))
361
{
362
sortColumns[i] = str2;
363
}
364
else if (str2.Equals(sortColumns[i]))
365
{
366
Array.Clear(sortColumns, i, 1);
367
}
368
}
369
return string.Join(",", sortColumns).Replace(",,", ",").TrimStart(",".ToCharArray());
370
}
371
372
protected void OnPagePicked(object sender, EventArgs e)
373
{
374
DropDownList list = (DropDownList)sender;
375
this.PageIndex = Convert.ToInt32(list.SelectedItem.Value) - 1;
376
GridViewPageEventArgs args = new GridViewPageEventArgs(Convert.ToInt32(list.SelectedItem.Value) - 1);
377
this.OnPageIndexChanging(args);
378
}
379
380
protected void OnbtnOK_Click(object sender, EventArgs e)
381
{
382
383
//if (((Button)sender).Text.Trim() != "")
384
//{
385
386
// int intPageNo = Convert.ToInt32(((Button)sender).Text.Trim())-1;
387
// if (intPageNo > 0 && intPageNo < this.PageCount)
388
// {
389
// this.PageIndex = intPageNo;
390
// GridViewPageEventArgs args = new GridViewPageEventArgs(intPageNo);
391
// this.OnPageIndexChanging(args);
392
// }
393
//}
394
}
395
396
protected void Pagination(object sender, EventArgs e)
397
{
398
LinkButton lbtn = (LinkButton)sender;
399
string PageArgs = lbtn.CommandArgument;
400
switch (PageArgs)
401
{
402
case "First":
403
this.PageIndex = 0;
404
break;
405
case "Prev":
406
if (this.PageIndex > 0)
407
{
408
this.PageIndex = this.PageIndex - 1;
409
}
410
break;
411
case "Next":
412
if (this.PageIndex < this.PageCount - 1)
413
{
414
this.PageIndex = this.PageIndex + 1;
415
}
416
break;
417
case "Last":
418
this.PageIndex = this.PageCount - 1;
419
break;
420
}
421
GridViewPageEventArgs args = new GridViewPageEventArgs(this.PageIndex);
422
this.OnPageIndexChanging(args);
423
424
425
}
426
427
protected override void OnRowCreated(GridViewRowEventArgs e)
428
{
429
GridViewRow row;
430
if (e.Row.RowType == DataControlRowType.Header)
431
{
432
if (this.AllowPagingToggle)
433
{
434
ImageButton child = new ImageButton();
435
child.AlternateText = "toggle";
436
child.ImageUrl = "~/images/toggle.gif";
437
child.Click += new ImageClickEventHandler(this.imgToggle_Click);
438
Image image = new Image();
439
image.ImageUrl = "~/images/spacer.gif";
440
image.Width = 10;
441
ImageButton button2 = new ImageButton();
442
button2.AlternateText = "excel";
443
button2.ImageUrl = "~/images/excel.gif";
444
button2.Click += new ImageClickEventHandler(this.btnExport_Click);
445
row = e.Row;
446
row.Cells[0].Controls.Add(child);
447
row.Cells[0].Controls.Add(image);
448
row.Cells[0].Controls.Add(button2);
449
}
450
if (this.SortExpression != string.Empty)
451
{
452
this.DisplaySortOrderImages(this.SortExpression, e.Row);
453
}
454
}
455
else if (e.Row.RowType == DataControlRowType.DataRow)
456
{
457
row = e.Row;
458
bool flag = (row.RowIndex % 2) == 0;
459
row.Attributes["onmouseover"] = "HandleOver( this );";
460
row.Attributes["onmouseout"] = "HandleOut( this );";
461
}
462
else if (e.Row.RowType == DataControlRowType.Pager)
463
{
464
this.CustomizePageBar(e);
465
}
466
base.OnRowCreated(e);
467
}
468
469
protected override void OnRowDataBound(GridViewRowEventArgs e)
470
{
471
if (e.Row.RowType == DataControlRowType.DataRow)
472
{
473
GridViewRow row = e.Row;
474
for (int i = 0; i < row.Cells.Count; i++)
475
{
476
if (!string.IsNullOrEmpty(row.Cells[i].Text) && char.IsNumber(row.Cells[i].Text[0]))
477
{
478
row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
479
}
480
}
481
}
482
base.OnRowDataBound(e);
483
}
484
485
protected override void OnSorting(GridViewSortEventArgs e)
486
{
487
if (this.AllowMultiColumnSorting)
488
{
489
e.SortExpression = this.GetSortExpression(e);
490
}
491
base.OnSorting(e);
492
}
493
494
protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
495
{
496
sortOrder = "";
497
sortOrderNo = -1;
498
for (int i = 0; i < sortColumns.Length; i++)
499
{
500
if (sortColumns[i].StartsWith(sortColumn))
501
{
502
sortOrderNo = i + 1;
503
if (this.AllowMultiColumnSorting)
504
{
505
sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
506
}
507
else
508
{
509
sortOrder = (this.SortDirection == SortDirection.Ascending) ? "ASC" : "DESC";
510
}
511
}
512
}
513
}
514
515
[ DefaultValue("false"),Description("Whether Sorting On more than one column is enabled"), Category("Behavior")]
516
public bool AllowMultiColumnSorting
517
{
518
get
519
{
520
object obj2 = this.ViewState["EnableMultiColumnSorting"];
521
return ((obj2 != null) ? ((bool)obj2) : false);
522
}
523
set
524
{
525
this.AllowSorting = true;
526
this.ViewState["EnableMultiColumnSorting"] = value;
527
}
528
}
529
530
[DefaultValue("true"), Description("Whether Paging Toggle is enabled"), Category("Behavior")]
531
public bool AllowPagingToggle
532
{
533
get
534
{
535
object obj2 = this.ViewState["AllowPagingToggle"];
536
return ((obj2 != null) ? ((bool)obj2) : false);
537
}
538
set
539
{
540
this.ViewState["AllowPagingToggle"] = value;
541
}
542
}
543
544
[DefaultValue(""), Editor("System.Web.UI.Design.UrlEditor", typeof(UITypeEditor)), Description("Image to display for Ascending Sort"), Category("Misc")]
545
public string SortAscImageUrl
546
{
547
get
548
{
549
object obj2 = this.ViewState["SortImageAsc"];
550
return ((obj2 != null) ? obj2.ToString() : "");
551
}
552
set
553
{
554
this.ViewState["SortImageAsc"] = value;
555
}
556
}
557
558
[DefaultValue(""), Editor("System.Web.UI.Design.UrlEditor", typeof(UITypeEditor)), Description("Image to display for Descending Sort"), Category("Misc")]
559
public string SortDescImageUrl
560
{
561
get
562
{
563
object obj2 = this.ViewState["SortImageDesc"];
564
return ((obj2 != null) ? obj2.ToString() : "");
565
}
566
set
567
{
568
this.ViewState["SortImageDesc"] = value;
569
}
570
}
571
}
572
}