自定义的GridView控件源代码

本文介绍了一个扩展GridView控件功能的方法,包括实现分页导航、导出数据到Excel等实用功能,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ContractedBlock.gifExpandedBlockStart.gifCode
  1None.gifnamespace KFC
  2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  3InBlock.gif    using System;
  4InBlock.gif    using System.ComponentModel;
  5InBlock.gif    using System.Drawing.Design;
  6InBlock.gif    using System.IO;
  7InBlock.gif    using System.Runtime.InteropServices;
  8InBlock.gif    using System.Web;
  9InBlock.gif    using System.Web.UI;
 10InBlock.gif    using System.Web.UI.HtmlControls;
 11InBlock.gif    using System.Web.UI.WebControls;
 12InBlock.gif
 13InBlock.gif    public class myGridView : GridView
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        protected void btnExport_Click(object sender, ImageClickEventArgs e)
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17InBlock.gif            ImageButton button = (ImageButton)sender;
 18InBlock.gif            GridView parent = (GridView)button.Parent.Parent.Parent.Parent;
 19InBlock.gif            HttpResponse response = this.Context.ApplicationInstance.Response;
 20InBlock.gif            if ((parent.Rows.Count + 1< 0x10000)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 22InBlock.gif                parent.AllowPaging = false;
 23InBlock.gif                parent.DataBind();
 24InBlock.gif                StringWriter writer = new StringWriter();
 25InBlock.gif                HtmlTextWriter writer2 = new HtmlTextWriter(writer);
 26InBlock.gif                HtmlForm child = new HtmlForm();
 27InBlock.gif                response.Charset = "gb2312";
 28InBlock.gif                response.ContentEncoding = System.Text.Encoding.UTF8;
 29InBlock.gif                response.ContentType = "application/vnd.ms-word";
 30InBlock.gif                response.AddHeader("content-disposition""attachment;filename=Report.doc");
 31InBlock.gif               
 32InBlock.gif             
 33InBlock.gif                this.EnableViewState = false;
 34InBlock.gif                this.Page.Controls.Add(child);
 35InBlock.gif                child.Controls.Add(parent);
 36InBlock.gif                this.ClearControls(parent);
 37InBlock.gif                child.RenderControl(writer2);
 38InBlock.gif                response.Write(writer.ToString());
 39InBlock.gif                response.End();
 40InBlock.gif                parent.AllowPaging = true;
 41InBlock.gif                parent.DataBind();
 42ExpandedSubBlockEnd.gif            }

 43InBlock.gif            else
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45InBlock.gif                response.Write("记录数量过大,无法导出到Excel。");
 46ExpandedSubBlockEnd.gif            }

 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif
 49InBlock.gif        protected void Button1_Click(object sender, EventArgs e)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            Button button = (Button)sender;
 52InBlock.gif            myGridView parent = (myGridView)button.Parent.Parent.Parent.Parent;
 53InBlock.gif            HttpResponse response = this.Context.ApplicationInstance.Response;
 54InBlock.gif            response.Clear();
 55InBlock.gif            response.AddHeader("content-disposition""attachment;filename=FileName.xls");
 56InBlock.gif            response.Charset = "gb2312";
 57InBlock.gif            response.ContentType = "application/vnd.xls";
 58InBlock.gif            StringWriter writer = new StringWriter();
 59InBlock.gif            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
 60InBlock.gif            parent.AllowPaging = false;
 61InBlock.gif            parent.DataBind();
 62InBlock.gif            parent.RenderControl(writer2);
 63InBlock.gif            response.Write(writer.ToString());
 64InBlock.gif            response.End();
 65InBlock.gif            parent.AllowPaging = true;
 66InBlock.gif            parent.DataBind();
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif
 69InBlock.gif        private void ClearControls(Control control)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            for (int i = control.Controls.Count - 1; i >= 0; i--)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 73InBlock.gif                this.ClearControls(control.Controls[i]);
 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            if ((control is ImageButton) || (control is Image))
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                control.Parent.Controls.Remove(control);
 78ExpandedSubBlockEnd.gif            }

 79InBlock.gif            if (control is TableCell)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 81InBlock.gif                for (int j = 0; j < control.Controls.Count; j++)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 83InBlock.gif                    LiteralControl control2;
 84InBlock.gif                    if ((control.Controls[j] is ImageButton) || (control.Controls[j] is Image))
 85ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 86InBlock.gif                        control.Controls.Remove(control.Controls[j]);
 87ExpandedSubBlockEnd.gif                    }

 88InBlock.gif                    if (control.Controls[j] is CheckBox)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 90InBlock.gif                        CheckBox box = (CheckBox) control.Controls[j];
 91InBlock.gif                        control2 = new LiteralControl();
 92InBlock.gif                        control2.Text = box.Checked ? "true" : "false";
 93InBlock.gif                        control.Controls.Add(control2);
 94InBlock.gif                        control.Controls.Remove(box);
 95ExpandedSubBlockEnd.gif                    }

 96InBlock.gif                    else if (!(control.Controls[j] is Label) && !(control.Controls[j] is LiteralControl))
 97ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 98InBlock.gif                        Control control3 = control.Controls[j];
 99InBlock.gif                        if (control3.GetType().GetProperty("SelectedItem"!= null)
100ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
101InBlock.gif                            control2 = new LiteralControl();
102InBlock.gif                            try
103ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
104InBlock.gif                                control2.Text = control3.GetType().GetProperty("SelectedItem").GetValue(control3, null).ToString();
105ExpandedSubBlockEnd.gif                            }

106InBlock.gif                            catch (Exception)
107ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
108ExpandedSubBlockEnd.gif                            }

109InBlock.gif                            control.Controls.Add(control2);
110InBlock.gif                            control.Controls.Remove(control3);
111ExpandedSubBlockEnd.gif                        }

112InBlock.gif                        else if (control3.GetType().GetProperty("Text"!= null)
113ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
114InBlock.gif                            control2 = new LiteralControl();
115InBlock.gif                            control2.Text = control3.GetType().GetProperty("Text").GetValue(control3, null).ToString();
116InBlock.gif                            control.Controls.Add(control2);
117InBlock.gif                            control.Controls.Remove(control3);
118ExpandedSubBlockEnd.gif                        }

119ExpandedSubBlockEnd.gif                    }

120ExpandedSubBlockEnd.gif                }

121ExpandedSubBlockEnd.gif            }

122ExpandedSubBlockEnd.gif        }

123InBlock.gif
124InBlock.gif        private void CustomizePageBar(GridViewRowEventArgs e)
125ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{        
126InBlock.gif
127InBlock.gif            Label label = new Label();
128InBlock.gif            label.Text = (this.PageIndex + 1+ "/" + this.PageCount;
129InBlock.gif            label.Font.Bold = true;
130InBlock.gif
131InBlock.gif            TableCell cell2 = new TableCell();
132InBlock.gif            cell2.Width = Unit.Pixel(80);
133InBlock.gif            cell2.Controls.Add(label);
134InBlock.gif
135ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*
136InBlock.gif            Label lbTotal = new Label();
137InBlock.gif            lbTotal.Font.Bold = true;
138InBlock.gif            int currentPageIndex = this.PageIndex;
139InBlock.gif            this.PageIndex = this.PageCount - 1;
140InBlock.gif
141InBlock.gif            lbTotal.Text = "共"+(this.PageCount - 1) * this.PageSize + this.Rows.Count+"条记录";
142InBlock.gif
143InBlock.gif            this.PageIndex = currentPageIndex;
144InBlock.gif
145InBlock.gif            TableCell cellTotal = new TableCell();
146InBlock.gif            cellTotal.Width = Unit.Pixel(120);
147InBlock.gif            cellTotal.Controls.Add(lbTotal);
148ExpandedSubBlockEnd.gif             */

149InBlock.gif             
150InBlock.gif
151InBlock.gif
152InBlock.gif            LinkButton lbtnFirst = new LinkButton();
153InBlock.gif            lbtnFirst.ID = "lbtnfirst";
154InBlock.gif            lbtnFirst.Text = "首  页";
155InBlock.gif            lbtnFirst.Font.Bold = true;
156InBlock.gif            lbtnFirst.Font.Size = base.PagerStyle.Font.Size;
157InBlock.gif            lbtnFirst.Click += new EventHandler(this.Pagination);
158InBlock.gif            lbtnFirst.CommandArgument = "First";
159InBlock.gif
160InBlock.gif            TableCell cellFirst = new TableCell();
161InBlock.gif            cellFirst.Width = Unit.Pixel(70);
162InBlock.gif            cellFirst.Controls.Add(lbtnFirst);
163InBlock.gif
164InBlock.gif            LinkButton lbtnPrev = new LinkButton();
165InBlock.gif            lbtnPrev.ID = "lbtnprev";
166InBlock.gif            lbtnPrev.Text = "上一页";
167InBlock.gif            lbtnPrev.Font.Bold = true;
168InBlock.gif            lbtnPrev.Font.Size = base.PagerStyle.Font.Size;
169InBlock.gif            lbtnPrev.Click+=new EventHandler(this.Pagination);
170InBlock.gif            lbtnPrev.CommandArgument = "Prev";
171InBlock.gif
172InBlock.gif            TableCell cellPrev = new TableCell();
173InBlock.gif            cellPrev.Width = Unit.Pixel(70);
174InBlock.gif            cellPrev.Controls.Add(lbtnPrev);
175InBlock.gif
176InBlock.gif            LinkButton lbtnNext = new LinkButton();
177InBlock.gif            lbtnNext.ID = "lbtnnext";
178InBlock.gif            lbtnNext.Text = "下一页";
179InBlock.gif            lbtnNext.Font.Bold = true;
180InBlock.gif            lbtnNext.Font.Size = base.PagerStyle.Font.Size;
181InBlock.gif            lbtnNext.Click+=new EventHandler(this.Pagination);
182InBlock.gif            lbtnNext.CommandArgument = "Next";
183InBlock.gif
184InBlock.gif            TableCell cellNext = new TableCell();
185InBlock.gif            cellNext.Width = Unit.Pixel(70);
186InBlock.gif            cellNext.Controls.Add(lbtnNext);
187InBlock.gif
188InBlock.gif            LinkButton lbtnLast = new LinkButton();
189InBlock.gif            lbtnLast.ID = "lbtnLast";
190InBlock.gif            lbtnLast.Text = "末  页";
191InBlock.gif            lbtnLast.Font.Bold = true;
192InBlock.gif            lbtnLast.Font.Size = base.PagerStyle.Font.Size;
193InBlock.gif            lbtnLast.Click+=new EventHandler(this.Pagination);
194InBlock.gif            lbtnLast.CommandArgument = "Last";
195InBlock.gif
196InBlock.gif            TableCell cellLast = new TableCell();
197InBlock.gif            cellLast.Width = Unit.Pixel(70);
198InBlock.gif            cellLast.Controls.Add(lbtnLast);
199InBlock.gif
200InBlock.gif            //cellFirst.Controls.Add(lbtnFirst);
201InBlock.gif            //cellFirst.Controls.Add(lbtnPrev);
202InBlock.gif            //cellFirst.Controls.Add(lbtnNext);
203InBlock.gif            //cellFirst.Controls.Add(lbtnLast);
204InBlock.gif
205InBlock.gif            
206InBlock.gif
207InBlock.gif            Label label2 = new Label();
208InBlock.gif            label2.Text = "go: ";
209InBlock.gif            label2.ID = "lblGoTo";
210InBlock.gif            label2.Font.Bold = true;
211InBlock.gif            label2.Font.Size = base.PagerStyle.Font.Size;
212InBlock.gif
213InBlock.gif            TableCell cell3 = new TableCell();
214InBlock.gif            //cell3.Width = Unit.Pixel(0x10);
215InBlock.gif
216InBlock.gif            cell3.Controls.Add(label2);
217InBlock.gif
218InBlock.gif
219InBlock.gif            DropDownList list = new DropDownList();
220InBlock.gif            list.ID = "ddlPick";
221InBlock.gif            list.AutoPostBack = true;
222InBlock.gif            list.EnableViewState = true;
223InBlock.gif            list.Font.Size = base.PagerStyle.Font.Size;
224InBlock.gif
225InBlock.gif            for (int i = 1; i <= this.PageCount; i++)
226ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
227InBlock.gif                list.Items.Add(i.ToString());
228ExpandedSubBlockEnd.gif            }

229InBlock.gif            list.SelectedIndex = this.PageIndex;
230InBlock.gif            list.SelectedIndexChanged += new EventHandler(this.OnPagePicked);
231InBlock.gif
232InBlock.gif            TextBox txtPage = new TextBox();
233InBlock.gif            txtPage.Text = "";
234InBlock.gif            txtPage.Width = Unit.Pixel(20);
235InBlock.gif            txtPage.EnableViewState = true;
236InBlock.gif            txtPage.Font.Size = base.PagerStyle.Font.Size;
237InBlock.gif
238InBlock.gif
239InBlock.gif            TableCell cell4 = new TableCell();
240InBlock.gif            //cell4.Width = Unit.Pixel(0x10);
241InBlock.gif            
242InBlock.gif
243InBlock.gif            Button btnOK = new Button();
244InBlock.gif            btnOK.ID = "btnok";
245InBlock.gif            btnOK.Text = "OK";
246InBlock.gif            btnOK.Click+=new EventHandler(this.OnbtnOK_Click);
247InBlock.gif
248InBlock.gif            cell4.Controls.Add(txtPage);
249InBlock.gif            cell4.Controls.Add(btnOK);
250InBlock.gif
251InBlock.gif
252InBlock.gif            TableCell cell5 = new TableCell();
253InBlock.gif            cell5.Width = Unit.Pixel(150);
254InBlock.gif
255InBlock.gif            foreach (Control control in e.Row.Cells[0].Controls)
256ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
257InBlock.gif                cell5.Controls.Add(control);
258ExpandedSubBlockEnd.gif            }

259InBlock.gif
260InBlock.gif            Table child = new Table();//
261InBlock.gif            child.BorderWidth = 0;
262InBlock.gif            child.CellPadding = 0;
263InBlock.gif            child.CellSpacing = 0;
264InBlock.gif            child.Width = Unit.Percentage(100.0);
265InBlock.gif            child.Height = Unit.Pixel(20);
266InBlock.gif
267InBlock.gif            child.Rows.Add(new TableRow());
268InBlock.gif
269InBlock.gif            child.Rows[0].Cells.Add(cell2);
270InBlock.gif
271InBlock.gif            //child.Rows[0].Cells.Add(cellTotal);
272InBlock.gif            child.Rows[0].Cells.Add(cellFirst);
273InBlock.gif            child.Rows[0].Cells.Add(cellPrev);
274InBlock.gif            child.Rows[0].Cells.Add(cellNext);
275InBlock.gif            child.Rows[0].Cells.Add(cellLast);
276InBlock.gif            
277InBlock.gif            child.Rows[0].Cells.Add(cell5);
278InBlock.gif            child.Rows[0].Cells.Add(cell3);
279InBlock.gif            child.Rows[0].Cells.Add(cell4);
280InBlock.gif            e.Row.Cells[0].Controls.Add(child);
281ExpandedSubBlockEnd.gif        }

282InBlock.gif
283InBlock.gif        protected void DisplaySortOrderImages(string sortExpression, GridViewRow dgItem)
284ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
285InBlock.gif            string[] sortColumns = sortExpression.Split(",".ToCharArray());
286InBlock.gif            for (int i = 0; i < dgItem.Cells.Count; i++)
287ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
288InBlock.gif                if ((dgItem.Cells[i].Controls.Count > 0&& (dgItem.Cells[i].Controls[0is LinkButton))
289ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
290InBlock.gif                    string str;
291InBlock.gif                    int num2;
292InBlock.gif                    string commandArgument = ((LinkButton)dgItem.Cells[i].Controls[0]).CommandArgument;
293InBlock.gif                    this.SearchSortExpression(sortColumns, commandArgument, out str, out num2);
294InBlock.gif                    if (num2 > 0)
295ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
296InBlock.gif                        string str3 = str.Equals("ASC"? this.SortAscImageUrl : this.SortDescImageUrl;
297InBlock.gif                        if (str3 != string.Empty)
298ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
299InBlock.gif                            Image child = new Image();
300InBlock.gif                            child.ImageUrl = str3;
301InBlock.gif                            dgItem.Cells[i].Controls.Add(child);
302InBlock.gif                            Label label = new Label();
303InBlock.gif                            label.Font.Size = FontUnit.Small;
304InBlock.gif                            label.Text = num2.ToString();
305InBlock.gif                            dgItem.Cells[i].Controls.Add(label);
306ExpandedSubBlockEnd.gif                        }

307InBlock.gif                        else
308ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
309InBlock.gif                            Label label2 = new Label();
310InBlock.gif                            label2.Font.Size = FontUnit.XSmall;
311InBlock.gif                            label2.Font.Name = "webdings";
312InBlock.gif                            label2.EnableTheming = false;
313InBlock.gif                            label2.Text = str.Equals("ASC"? "5" : "6";
314InBlock.gif                            dgItem.Cells[i].Controls.Add(label2);
315InBlock.gif                            if (this.AllowMultiColumnSorting)
316ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
317InBlock.gif                                Literal literal = new Literal();
318InBlock.gif                                literal.Text = num2.ToString();
319InBlock.gif                                dgItem.Cells[i].Controls.Add(literal);
320ExpandedSubBlockEnd.gif                            }

321ExpandedSubBlockEnd.gif                        }

322ExpandedSubBlockEnd.gif                    }

323ExpandedSubBlockEnd.gif                }

324ExpandedSubBlockEnd.gif            }

325ExpandedSubBlockEnd.gif        }

326InBlock.gif
327InBlock.gif        protected string GetSortExpression(GridViewSortEventArgs e)
328ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
329InBlock.gif            string[] sortColumns = null;
330InBlock.gif            string sortExpression = this.SortExpression;
331InBlock.gif            if (sortExpression != string.Empty)
332ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
333InBlock.gif                sortColumns = sortExpression.Split(",".ToCharArray());
334ExpandedSubBlockEnd.gif            }

335InBlock.gif            if ((sortExpression.IndexOf(e.SortExpression) > 0|| sortExpression.StartsWith(e.SortExpression))
336ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
337InBlock.gif                sortExpression = this.ModifySortExpression(sortColumns, e.SortExpression);
338ExpandedSubBlockEnd.gif            }

339InBlock.gif            else
340ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
341InBlock.gif                sortExpression = sortExpression + ("," + e.SortExpression + " ASC ");
342ExpandedSubBlockEnd.gif            }

343InBlock.gif            return sortExpression.TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
344ExpandedSubBlockEnd.gif        }

345InBlock.gif
346InBlock.gif        protected void imgToggle_Click(object sender, ImageClickEventArgs e)
347ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
348InBlock.gif            ImageButton button = (ImageButton)sender;
349InBlock.gif            GridView parent = (GridView)button.Parent.Parent.Parent.Parent;
350InBlock.gif            
351InBlock.gif            parent.AllowPaging = !parent.AllowPaging;
352ExpandedSubBlockEnd.gif        }

353InBlock.gif
354InBlock.gif        protected string ModifySortExpression(string[] sortColumns, string sortExpression)
355ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
356InBlock.gif            string str = sortExpression + " ASC ";
357InBlock.gif            string str2 = sortExpression + " DESC ";
358InBlock.gif            for (int i = 0; i < sortColumns.Length; i++)
359ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
360InBlock.gif                if (str.Equals(sortColumns[i]))
361ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
362InBlock.gif                    sortColumns[i] = str2;
363ExpandedSubBlockEnd.gif                }

364InBlock.gif                else if (str2.Equals(sortColumns[i]))
365ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
366InBlock.gif                    Array.Clear(sortColumns, i, 1);
367ExpandedSubBlockEnd.gif                }

368ExpandedSubBlockEnd.gif            }

369InBlock.gif            return string.Join(",", sortColumns).Replace(",,"",").TrimStart(",".ToCharArray());
370ExpandedSubBlockEnd.gif        }

371InBlock.gif
372InBlock.gif        protected void OnPagePicked(object sender, EventArgs e)
373ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
374InBlock.gif            DropDownList list = (DropDownList)sender;
375InBlock.gif            this.PageIndex = Convert.ToInt32(list.SelectedItem.Value) - 1;
376InBlock.gif            GridViewPageEventArgs args = new GridViewPageEventArgs(Convert.ToInt32(list.SelectedItem.Value) - 1);
377InBlock.gif            this.OnPageIndexChanging(args);
378ExpandedSubBlockEnd.gif        }

379InBlock.gif
380InBlock.gif        protected void OnbtnOK_Click(object sender, EventArgs e)
381ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
382InBlock.gif            
383InBlock.gif            //if (((Button)sender).Text.Trim() != "")
384InBlock.gif            //{
385InBlock.gif                
386InBlock.gif            //    int intPageNo = Convert.ToInt32(((Button)sender).Text.Trim())-1;
387InBlock.gif            //    if (intPageNo > 0 && intPageNo < this.PageCount)
388InBlock.gif            //    {
389InBlock.gif            //        this.PageIndex = intPageNo;
390InBlock.gif            //        GridViewPageEventArgs args = new GridViewPageEventArgs(intPageNo);
391InBlock.gif            //        this.OnPageIndexChanging(args);
392InBlock.gif            //    }
393InBlock.gif            //}
394ExpandedSubBlockEnd.gif        }

395InBlock.gif
396InBlock.gif        protected void Pagination(object sender, EventArgs e)
397ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
398InBlock.gif            LinkButton lbtn = (LinkButton)sender;
399InBlock.gif            string PageArgs = lbtn.CommandArgument;
400InBlock.gif            switch (PageArgs)
401ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
402InBlock.gif                case "First":
403InBlock.gif                    this.PageIndex = 0;
404InBlock.gif                    break;
405InBlock.gif                case "Prev":
406InBlock.gif                    if (this.PageIndex > 0)
407ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
408InBlock.gif                        this.PageIndex = this.PageIndex - 1;
409ExpandedSubBlockEnd.gif                    }

410InBlock.gif                    break;
411InBlock.gif                case "Next":
412InBlock.gif                    if (this.PageIndex < this.PageCount - 1)
413ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
414InBlock.gif                        this.PageIndex = this.PageIndex + 1;
415ExpandedSubBlockEnd.gif                    }

416InBlock.gif                    break;
417InBlock.gif                case "Last":
418InBlock.gif                    this.PageIndex = this.PageCount - 1;
419InBlock.gif                    break;
420ExpandedSubBlockEnd.gif            }

421InBlock.gif            GridViewPageEventArgs args = new GridViewPageEventArgs(this.PageIndex);
422InBlock.gif            this.OnPageIndexChanging(args);
423InBlock.gif
424InBlock.gif            
425ExpandedSubBlockEnd.gif        }

426InBlock.gif
427InBlock.gif        protected override void OnRowCreated(GridViewRowEventArgs e)
428ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
429InBlock.gif            GridViewRow row;
430InBlock.gif            if (e.Row.RowType == DataControlRowType.Header)
431ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
432InBlock.gif                if (this.AllowPagingToggle)
433ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
434InBlock.gif                    ImageButton child = new ImageButton();
435InBlock.gif                    child.AlternateText = "toggle";
436InBlock.gif                    child.ImageUrl = "~/images/toggle.gif";
437InBlock.gif                    child.Click += new ImageClickEventHandler(this.imgToggle_Click);
438InBlock.gif                    Image image = new Image();
439InBlock.gif                    image.ImageUrl = "~/images/spacer.gif";
440InBlock.gif                    image.Width = 10;
441InBlock.gif                    ImageButton button2 = new ImageButton();
442InBlock.gif                    button2.AlternateText = "excel";
443InBlock.gif                    button2.ImageUrl = "~/images/excel.gif";
444InBlock.gif                    button2.Click += new ImageClickEventHandler(this.btnExport_Click);
445InBlock.gif                    row = e.Row;
446InBlock.gif                    row.Cells[0].Controls.Add(child);
447InBlock.gif                    row.Cells[0].Controls.Add(image);
448InBlock.gif                    row.Cells[0].Controls.Add(button2);
449ExpandedSubBlockEnd.gif                }

450InBlock.gif                if (this.SortExpression != string.Empty)
451ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
452InBlock.gif                    this.DisplaySortOrderImages(this.SortExpression, e.Row);
453ExpandedSubBlockEnd.gif                }

454ExpandedSubBlockEnd.gif            }

455InBlock.gif            else if (e.Row.RowType == DataControlRowType.DataRow)
456ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
457InBlock.gif                row = e.Row;
458InBlock.gif                bool flag = (row.RowIndex % 2== 0;
459InBlock.gif                row.Attributes["onmouseover"= "HandleOver( this );";
460InBlock.gif                row.Attributes["onmouseout"= "HandleOut( this );";
461ExpandedSubBlockEnd.gif            }

462InBlock.gif            else if (e.Row.RowType == DataControlRowType.Pager)
463ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
464InBlock.gif                this.CustomizePageBar(e);
465ExpandedSubBlockEnd.gif            }

466InBlock.gif            base.OnRowCreated(e);
467ExpandedSubBlockEnd.gif        }

468InBlock.gif
469InBlock.gif        protected override void OnRowDataBound(GridViewRowEventArgs e)
470ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
471InBlock.gif            if (e.Row.RowType == DataControlRowType.DataRow)
472ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
473InBlock.gif                GridViewRow row = e.Row;
474InBlock.gif                for (int i = 0; i < row.Cells.Count; i++)
475ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
476InBlock.gif                    if (!string.IsNullOrEmpty(row.Cells[i].Text) && char.IsNumber(row.Cells[i].Text[0]))
477ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
478InBlock.gif                        row.Cells[i].Attributes.Add("style""vnd.ms-excel.numberformat:@");
479ExpandedSubBlockEnd.gif                    }

480ExpandedSubBlockEnd.gif                }

481ExpandedSubBlockEnd.gif            }

482InBlock.gif            base.OnRowDataBound(e);
483ExpandedSubBlockEnd.gif        }

484InBlock.gif
485InBlock.gif        protected override void OnSorting(GridViewSortEventArgs e)
486ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
487InBlock.gif            if (this.AllowMultiColumnSorting)
488ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
489InBlock.gif                e.SortExpression = this.GetSortExpression(e);
490ExpandedSubBlockEnd.gif            }

491InBlock.gif            base.OnSorting(e);
492ExpandedSubBlockEnd.gif        }

493InBlock.gif
494InBlock.gif        protected void SearchSortExpression(string[] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
495ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
496InBlock.gif            sortOrder = "";
497InBlock.gif            sortOrderNo = -1;
498InBlock.gif            for (int i = 0; i < sortColumns.Length; i++)
499ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
500InBlock.gif                if (sortColumns[i].StartsWith(sortColumn))
501ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
502InBlock.gif                    sortOrderNo = i + 1;
503InBlock.gif                    if (this.AllowMultiColumnSorting)
504ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
505InBlock.gif                        sortOrder = sortColumns[i].Substring(sortColumn.Length).Trim();
506ExpandedSubBlockEnd.gif                    }

507InBlock.gif                    else
508ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
509InBlock.gif                        sortOrder = (this.SortDirection == SortDirection.Ascending) ? "ASC" : "DESC";
510ExpandedSubBlockEnd.gif                    }

511ExpandedSubBlockEnd.gif                }

512ExpandedSubBlockEnd.gif            }

513ExpandedSubBlockEnd.gif        }

514InBlock.gif
515InBlock.gif        [ DefaultValue("false"),Description("Whether Sorting On more than one column is enabled"), Category("Behavior")]
516InBlock.gif        public bool AllowMultiColumnSorting
517ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
518InBlock.gif            get
519ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
520InBlock.gif                object obj2 = this.ViewState["EnableMultiColumnSorting"];
521InBlock.gif                return ((obj2 != null? ((bool)obj2) : false);
522ExpandedSubBlockEnd.gif            }

523InBlock.gif            set
524ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
525InBlock.gif                this.AllowSorting = true;
526InBlock.gif                this.ViewState["EnableMultiColumnSorting"= value;
527ExpandedSubBlockEnd.gif            }

528ExpandedSubBlockEnd.gif        }

529InBlock.gif
530InBlock.gif        [DefaultValue("true"), Description("Whether Paging Toggle is enabled"), Category("Behavior")]
531InBlock.gif        public bool AllowPagingToggle
532ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
533InBlock.gif            get
534ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
535InBlock.gif                object obj2 = this.ViewState["AllowPagingToggle"];
536InBlock.gif                return ((obj2 != null? ((bool)obj2) : false);
537ExpandedSubBlockEnd.gif            }

538InBlock.gif            set
539ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
540InBlock.gif                this.ViewState["AllowPagingToggle"= value;
541ExpandedSubBlockEnd.gif            }

542ExpandedSubBlockEnd.gif        }

543InBlock.gif
544InBlock.gif        [DefaultValue(""), Editor("System.Web.UI.Design.UrlEditor"typeof(UITypeEditor)), Description("Image to display for Ascending Sort"), Category("Misc")]
545InBlock.gif        public string SortAscImageUrl
546ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
547InBlock.gif            get
548ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
549InBlock.gif                object obj2 = this.ViewState["SortImageAsc"];
550InBlock.gif                return ((obj2 != null? obj2.ToString() : "");
551ExpandedSubBlockEnd.gif            }

552InBlock.gif            set
553ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
554InBlock.gif                this.ViewState["SortImageAsc"= value;
555ExpandedSubBlockEnd.gif            }

556ExpandedSubBlockEnd.gif        }

557InBlock.gif
558InBlock.gif        [DefaultValue(""), Editor("System.Web.UI.Design.UrlEditor"typeof(UITypeEditor)), Description("Image to display for Descending Sort"), Category("Misc")]
559InBlock.gif        public string SortDescImageUrl
560ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
561InBlock.gif            get
562ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
563InBlock.gif                object obj2 = this.ViewState["SortImageDesc"];
564InBlock.gif                return ((obj2 != null? obj2.ToString() : "");
565ExpandedSubBlockEnd.gif            }

566InBlock.gif            set
567ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
568InBlock.gif                this.ViewState["SortImageDesc"= value;
569ExpandedSubBlockEnd.gif            }

570ExpandedSubBlockEnd.gif        }

571ExpandedSubBlockEnd.gif    }

572ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/shajianheng/archive/2008/12/24/1361190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值