增强 GridView 控件的功能
还记得雅虎的XX(不好意思,忘记姓甚名谁了)有一篇演讲,其中一个字就是“懒”。咱写代码的,不懒点儿还真是吃不消,尤其是现在这样要命的天气~~。
相信大家都多少有抱怨GridView控件的功能吧,咱在这就不多说了,贴出代码是最重要的。

该类增强了微软的GridView的功能,增加了“首页”、“上页”、“下页”、“尾页”按钮,方便大家使用,翻页事件也已经添加好,在相关页面只要添加以下代码就可以了:
记住必须在 Page_Init 里面。其中 listTable 为GridView控件的ID,BindData是自己绑定数据的方法的名称。
 using System;
using System;
 using System.Web;
using System.Web;
 using System.Web.UI;
using System.Web.UI;
 using System.Web.UI.WebControls;
using System.Web.UI.WebControls;

 namespace Lyout.Web.Extension {
namespace Lyout.Web.Extension {
 /// <summary>
    /// <summary>
 /// GridView 数据绑定委托
    /// GridView 数据绑定委托
 /// </summary>
    /// </summary>
 public delegate void GridViewDataBind();
    public delegate void GridViewDataBind();

 /// <summary>
    /// <summary>
 /// 增强 GridView 的功能
    /// 增强 GridView 的功能
 /// </summary>
    /// </summary>
 public sealed class GridView {
    public sealed class GridView {
 /// <summary>
        /// <summary>
 /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
 /// </summary>
        /// </summary>
 /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param>
 public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
 RegisterEvents(gridView, null, true);
            RegisterEvents(gridView, null, true);
 }
        }

 /// <summary>
        /// <summary>
 /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
 /// </summary>
        /// </summary>
 /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param>
 /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param>
 public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
 RegisterEvents(gridView, dataBind, true);
            RegisterEvents(gridView, dataBind, true);
 }
        }

 /// <summary>
        /// <summary>
 /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
 /// </summary>
        /// </summary>
 /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param>
 /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param>
 /// <param name="pageText">翻页按钮上的文字</param>
        /// <param name="pageText">翻页按钮上的文字</param>
 public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
 RegisterEvents(gridView, dataBind, pageText, true);
            RegisterEvents(gridView, dataBind, pageText, true);
 }
        }

 /// <summary>
        /// <summary>
 /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
 /// </summary>
        /// </summary>
 /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param>
 /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param>
 /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
        /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
 public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
 RegisterEvents(gridView, dataBind, null, autoChangePage);
            RegisterEvents(gridView, dataBind, null, autoChangePage);
 }
        }

 /// <summary>
        /// <summary>
 /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
 /// </summary>
        /// </summary>
 /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param>
 /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param>
 /// <param name="pageText">翻页按钮上的文字</param>
        /// <param name="pageText">翻页按钮上的文字</param>
 /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
        /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
 public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) {
 gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) {
            gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) {
 RowCommand(sender, e, dataBind);
                RowCommand(sender, e, dataBind);
 };
            };
 gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) {
            gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) {
 RowCreated(sender, e, pageText);
                RowCreated(sender, e, pageText);
 };
            };
 if (autoChangePage) {
            if (autoChangePage) {
 gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) {
                gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) {
 gridView.PageIndex = e.NewPageIndex;
                    gridView.PageIndex = e.NewPageIndex;
 if (dataBind != null) {
                    if (dataBind != null) {
 dataBind();
                        dataBind();
 } else {
                    } else {
 gridView.DataBind();
                        gridView.DataBind();
 }
                    }
 };
                };
 }
            }
 }
        }

 /// <summary>
        /// <summary>
 /// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
        /// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
 /// </summary>
        /// </summary>
 public static void RowCommand(object sender, GridViewCommandEventArgs e) {
        public static void RowCommand(object sender, GridViewCommandEventArgs e) {
 RowCommand(sender, e, null);
            RowCommand(sender, e, null);
 }
        }

 /// <summary>
        /// <summary>
 /// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
        /// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
 /// </summary>
        /// </summary>
 /// <param name="function">数据绑定方法</param>
        /// <param name="function">数据绑定方法</param>
 public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) {
        public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) {
 System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
            System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
 SetPageIndex(_grid, e.CommandName);
            SetPageIndex(_grid, e.CommandName);
 if (dataBind != null) {
            if (dataBind != null) {
 dataBind();
                dataBind();
 } else {
            } else {
 _grid.DataBind();
                _grid.DataBind();
 }
            }
 }
        }

 /// <summary>
        /// <summary>
 /// 增加首页、上页、下页、尾页按钮
        /// 增加首页、上页、下页、尾页按钮
 /// </summary>
        /// </summary>
 public static void RowCreated(object sender, GridViewRowEventArgs e) {
        public static void RowCreated(object sender, GridViewRowEventArgs e) {
 RowCreated(sender, e, null);
            RowCreated(sender, e, null);
 }
        }

 /// <summary>
        /// <summary>
 /// 增加首页、上页、下页、尾页按钮
        /// 增加首页、上页、下页、尾页按钮
 /// </summary>
        /// </summary>
 /// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>
        /// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>
 public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) {
        public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) {
 if(e.Row.RowType == DataControlRowType.Pager) {
            if(e.Row.RowType == DataControlRowType.Pager) {
 System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
                System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
 if(_grid.PagerTemplate == null) {
                if(_grid.PagerTemplate == null) {
 _grid.PagerSettings.Mode = PagerButtons.Numeric;
                    _grid.PagerSettings.Mode = PagerButtons.Numeric;

 int pageIndex = _grid.PageIndex;
                    int pageIndex = _grid.PageIndex;
 int pageCount = _grid.PageCount;
                    int pageCount = _grid.PageCount;

 TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
                    TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
 TableCell objCell;
                    TableCell objCell;

 string[] button = {"首页","上页","下页","尾页"};
                    string[] button = {"首页","上页","下页","尾页"};
 if(buttonText != null) {
                    if(buttonText != null) {
 for(int i = 0; i < buttonText.Length; i++) {
                        for(int i = 0; i < buttonText.Length; i++) {
 button[i] = buttonText[i];
                            button[i] = buttonText[i];
 }
                        }
 }
                    }

 bool enabled = true;
                    bool enabled = true;

 enabled = pageIndex > 0 ? true : false;
                    enabled = pageIndex > 0 ? true : false;

 // 增加首页、上一页按钮
                    // 增加首页、上一页按钮
 objCell = new TableCell();
                    objCell = new TableCell();
 objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled));
                    objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled));
 objRow.Cells.AddAt(0, objCell);
                    objRow.Cells.AddAt(0, objCell);

 objCell = new TableCell();
                    objCell = new TableCell();
 objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled));
                    objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled));
 objRow.Cells.AddAt(1, objCell);
                    objRow.Cells.AddAt(1, objCell);

 enabled = pageIndex < (pageCount - 1) ? true : false;
                    enabled = pageIndex < (pageCount - 1) ? true : false;

 // 增加尾页、下一页按钮
                    // 增加尾页、下一页按钮
 objCell = new TableCell();
                    objCell = new TableCell();
 objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled));
                    objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled));
 objRow.Cells.Add(objCell);
                    objRow.Cells.Add(objCell);

 objCell = new TableCell();
                    objCell = new TableCell();
 objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled));
                    objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled));
 objRow.Cells.Add(objCell);
                    objRow.Cells.Add(objCell);
 }
                }
 }
            }
 }
        }

 /// <summary>
        /// <summary>
 /// 设置GridView的页索引
        /// 设置GridView的页索引
 /// </summary>
        /// </summary>
 /// <param name="command">被单击的按钮的命令</param>
        /// <param name="command">被单击的按钮的命令</param>
 private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
        private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
 switch(command) {
            switch(command) {
 case "Page$First":
                case "Page$First":
 gridView.PageIndex = 0;
                    gridView.PageIndex = 0;
 break;
                    break;
 case "Page$Prev":
                case "Page$Prev":
 if(gridView.PageIndex > 0)
                    if(gridView.PageIndex > 0)
 gridView.PageIndex -= 1;
                        gridView.PageIndex -= 1;
 break;
                    break;
 case "Page$Next":
                case "Page$Next":
 if(gridView.PageIndex < (gridView.PageCount - 1))
                    if(gridView.PageIndex < (gridView.PageCount - 1))
 gridView.PageIndex += 1;
                        gridView.PageIndex += 1;
 break;
                    break;
 case "Page$Last":
                case "Page$Last":
 gridView.PageIndex = gridView.PageCount - 1;
                    gridView.PageIndex = gridView.PageCount - 1;
 break;
                    break;
 default:
                default:
 return;
                    return;
 }
            }
 }
        }

 /// <summary>
        /// <summary>
 /// 一个新的LinkButton对象
        /// 一个新的LinkButton对象
 /// </summary>
        /// </summary>
 /// <param name="text">文本</param>
        /// <param name="text">文本</param>
 /// <param name="cmd">相应命令</param>
        /// <param name="cmd">相应命令</param>
 /// <param name="css">Css风格</param>
        /// <param name="css">Css风格</param>
 /// <param name="enabled">可用</param>
        /// <param name="enabled">可用</param>
 /// <returns>LinkButton</returns>
        /// <returns>LinkButton</returns>
 private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) {
        private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) {
 LinkButton button = new LinkButton();
            LinkButton button = new LinkButton();
 button.Text = text;
            button.Text = text;
 button.CommandName = cmd;
            button.CommandName = cmd;
 button.CssClass = css;
            button.CssClass = css;
 button.Enabled = enabled;
            button.Enabled = enabled;
 button.CausesValidation = false;
            button.CausesValidation = false;
 return button;
            return button;
 }
        }
 }
    }
 }
}
相信大家都多少有抱怨GridView控件的功能吧,咱在这就不多说了,贴出代码是最重要的。

该类增强了微软的GridView的功能,增加了“首页”、“上页”、“下页”、“尾页”按钮,方便大家使用,翻页事件也已经添加好,在相关页面只要添加以下代码就可以了:
    protected void Page_Init(object sender, EventArgs e) {
Lyout.Web.Extension.GridView.RegisterEvents(listTable, new Lyout.Web.Extension.GridViewDataBind(BindData));
}
Lyout.Web.Extension.GridView.RegisterEvents(listTable, new Lyout.Web.Extension.GridViewDataBind(BindData));
}
记住必须在 Page_Init 里面。其中 listTable 为GridView控件的ID,BindData是自己绑定数据的方法的名称。
 using System;
using System; using System.Web;
using System.Web; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
 namespace Lyout.Web.Extension {
namespace Lyout.Web.Extension { /// <summary>
    /// <summary> /// GridView 数据绑定委托
    /// GridView 数据绑定委托 /// </summary>
    /// </summary> public delegate void GridViewDataBind();
    public delegate void GridViewDataBind();
 /// <summary>
    /// <summary> /// 增强 GridView 的功能
    /// 增强 GridView 的功能 /// </summary>
    /// </summary> public sealed class GridView {
    public sealed class GridView { /// <summary>
        /// <summary> /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件 /// </summary>
        /// </summary> /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param> public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) { RegisterEvents(gridView, null, true);
            RegisterEvents(gridView, null, true); }
        }
 /// <summary>
        /// <summary> /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件 /// </summary>
        /// </summary> /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param> /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param> public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) { RegisterEvents(gridView, dataBind, true);
            RegisterEvents(gridView, dataBind, true); }
        }
 /// <summary>
        /// <summary> /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件 /// </summary>
        /// </summary> /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param> /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param> /// <param name="pageText">翻页按钮上的文字</param>
        /// <param name="pageText">翻页按钮上的文字</param> public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) { RegisterEvents(gridView, dataBind, pageText, true);
            RegisterEvents(gridView, dataBind, pageText, true); }
        }
 /// <summary>
        /// <summary> /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件 /// </summary>
        /// </summary> /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param> /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param> /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
        /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param> public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) { RegisterEvents(gridView, dataBind, null, autoChangePage);
            RegisterEvents(gridView, dataBind, null, autoChangePage); }
        }
 /// <summary>
        /// <summary> /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
        /// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件 /// </summary>
        /// </summary> /// <param name="gridView">需要注册事件的GridView控件</param>
        /// <param name="gridView">需要注册事件的GridView控件</param> /// <param name="dataBind">数据绑定方法</param>
        /// <param name="dataBind">数据绑定方法</param> /// <param name="pageText">翻页按钮上的文字</param>
        /// <param name="pageText">翻页按钮上的文字</param> /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
        /// <param name="autoChangePage">是否自动添加点击页码的相应事件</param> public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) {
        public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) { gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) {
            gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) { RowCommand(sender, e, dataBind);
                RowCommand(sender, e, dataBind); };
            }; gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) {
            gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) { RowCreated(sender, e, pageText);
                RowCreated(sender, e, pageText); };
            }; if (autoChangePage) {
            if (autoChangePage) { gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) {
                gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) { gridView.PageIndex = e.NewPageIndex;
                    gridView.PageIndex = e.NewPageIndex; if (dataBind != null) {
                    if (dataBind != null) { dataBind();
                        dataBind(); } else {
                    } else { gridView.DataBind();
                        gridView.DataBind(); }
                    } };
                }; }
            } }
        }
 /// <summary>
        /// <summary> /// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
        /// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。 /// </summary>
        /// </summary> public static void RowCommand(object sender, GridViewCommandEventArgs e) {
        public static void RowCommand(object sender, GridViewCommandEventArgs e) { RowCommand(sender, e, null);
            RowCommand(sender, e, null); }
        }
 /// <summary>
        /// <summary> /// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
        /// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。 /// </summary>
        /// </summary> /// <param name="function">数据绑定方法</param>
        /// <param name="function">数据绑定方法</param> public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) {
        public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) { System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
            System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender; SetPageIndex(_grid, e.CommandName);
            SetPageIndex(_grid, e.CommandName); if (dataBind != null) {
            if (dataBind != null) { dataBind();
                dataBind(); } else {
            } else { _grid.DataBind();
                _grid.DataBind(); }
            } }
        }
 /// <summary>
        /// <summary> /// 增加首页、上页、下页、尾页按钮
        /// 增加首页、上页、下页、尾页按钮 /// </summary>
        /// </summary> public static void RowCreated(object sender, GridViewRowEventArgs e) {
        public static void RowCreated(object sender, GridViewRowEventArgs e) { RowCreated(sender, e, null);
            RowCreated(sender, e, null); }
        }
 /// <summary>
        /// <summary> /// 增加首页、上页、下页、尾页按钮
        /// 增加首页、上页、下页、尾页按钮 /// </summary>
        /// </summary> /// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>
        /// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param> public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) {
        public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) { if(e.Row.RowType == DataControlRowType.Pager) {
            if(e.Row.RowType == DataControlRowType.Pager) { System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
                System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender; if(_grid.PagerTemplate == null) {
                if(_grid.PagerTemplate == null) { _grid.PagerSettings.Mode = PagerButtons.Numeric;
                    _grid.PagerSettings.Mode = PagerButtons.Numeric;
 int pageIndex = _grid.PageIndex;
                    int pageIndex = _grid.PageIndex; int pageCount = _grid.PageCount;
                    int pageCount = _grid.PageCount;
 TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
                    TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0]; TableCell objCell;
                    TableCell objCell;
 string[] button = {"首页","上页","下页","尾页"};
                    string[] button = {"首页","上页","下页","尾页"}; if(buttonText != null) {
                    if(buttonText != null) { for(int i = 0; i < buttonText.Length; i++) {
                        for(int i = 0; i < buttonText.Length; i++) { button[i] = buttonText[i];
                            button[i] = buttonText[i]; }
                        } }
                    }
 bool enabled = true;
                    bool enabled = true;
 enabled = pageIndex > 0 ? true : false;
                    enabled = pageIndex > 0 ? true : false;
 // 增加首页、上一页按钮
                    // 增加首页、上一页按钮 objCell = new TableCell();
                    objCell = new TableCell(); objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled));
                    objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled)); objRow.Cells.AddAt(0, objCell);
                    objRow.Cells.AddAt(0, objCell);
 objCell = new TableCell();
                    objCell = new TableCell(); objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled));
                    objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled)); objRow.Cells.AddAt(1, objCell);
                    objRow.Cells.AddAt(1, objCell);
 enabled = pageIndex < (pageCount - 1) ? true : false;
                    enabled = pageIndex < (pageCount - 1) ? true : false;
 // 增加尾页、下一页按钮
                    // 增加尾页、下一页按钮 objCell = new TableCell();
                    objCell = new TableCell(); objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled));
                    objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled)); objRow.Cells.Add(objCell);
                    objRow.Cells.Add(objCell);
 objCell = new TableCell();
                    objCell = new TableCell(); objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled));
                    objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled)); objRow.Cells.Add(objCell);
                    objRow.Cells.Add(objCell); }
                } }
            } }
        }
 /// <summary>
        /// <summary> /// 设置GridView的页索引
        /// 设置GridView的页索引 /// </summary>
        /// </summary> /// <param name="command">被单击的按钮的命令</param>
        /// <param name="command">被单击的按钮的命令</param> private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
        private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) { switch(command) {
            switch(command) { case "Page$First":
                case "Page$First": gridView.PageIndex = 0;
                    gridView.PageIndex = 0; break;
                    break; case "Page$Prev":
                case "Page$Prev": if(gridView.PageIndex > 0)
                    if(gridView.PageIndex > 0) gridView.PageIndex -= 1;
                        gridView.PageIndex -= 1; break;
                    break; case "Page$Next":
                case "Page$Next": if(gridView.PageIndex < (gridView.PageCount - 1))
                    if(gridView.PageIndex < (gridView.PageCount - 1)) gridView.PageIndex += 1;
                        gridView.PageIndex += 1; break;
                    break; case "Page$Last":
                case "Page$Last": gridView.PageIndex = gridView.PageCount - 1;
                    gridView.PageIndex = gridView.PageCount - 1; break;
                    break; default:
                default: return;
                    return; }
            } }
        }
 /// <summary>
        /// <summary> /// 一个新的LinkButton对象
        /// 一个新的LinkButton对象 /// </summary>
        /// </summary> /// <param name="text">文本</param>
        /// <param name="text">文本</param> /// <param name="cmd">相应命令</param>
        /// <param name="cmd">相应命令</param> /// <param name="css">Css风格</param>
        /// <param name="css">Css风格</param> /// <param name="enabled">可用</param>
        /// <param name="enabled">可用</param> /// <returns>LinkButton</returns>
        /// <returns>LinkButton</returns> private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) {
        private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) { LinkButton button = new LinkButton();
            LinkButton button = new LinkButton(); button.Text = text;
            button.Text = text; button.CommandName = cmd;
            button.CommandName = cmd; button.CssClass = css;
            button.CssClass = css; button.Enabled = enabled;
            button.Enabled = enabled; button.CausesValidation = false;
            button.CausesValidation = false; return button;
            return button; }
        } }
    } }
} 
                    
                


 
     
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号