还记得雅虎的XX(不好意思,忘记姓甚名谁了)有一篇演讲,其中一个字就是“懒”。咱写代码的,不懒点儿还真是吃不消,尤其是现在这样要命的天气~~。
相信大家都多少有抱怨GridView控件的功能吧,咱在这就不多说了,贴出代码是最重要的。
该类增强了微软的GridView的功能,增加了“首页”、“上页”、“下页”、“尾页”按钮,方便大家使用,翻页事件也已经添加好,在相关页面只要添加以下代码就可以了:
记住必须在 Page_Init 里面。其中 listTable 为GridView控件的ID,BindData是自己绑定数据的方法的名称。
相信大家都多少有抱怨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.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lyout.Web.Extension {
/**//// <summary>
/// GridView 数据绑定委托
/// </summary>
public delegate void GridViewDataBind();
/**//// <summary>
/// 增强 GridView 的功能
/// </summary>
public sealed class GridView {
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
RegisterEvents(gridView, null, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
RegisterEvents(gridView, dataBind, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="pageText">翻页按钮上的文字</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
RegisterEvents(gridView, dataBind, pageText, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
RegisterEvents(gridView, dataBind, null, autoChangePage);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="pageText">翻页按钮上的文字</param>
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) {
gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) {
RowCommand(sender, e, dataBind);
};
gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) {
RowCreated(sender, e, pageText);
};
if (autoChangePage) {
gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) {
gridView.PageIndex = e.NewPageIndex;
if (dataBind != null) {
dataBind();
} else {
gridView.DataBind();
}
};
}
}
/**//// <summary>
/// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
/// </summary>
public static void RowCommand(object sender, GridViewCommandEventArgs e) {
RowCommand(sender, e, null);
}
/**//// <summary>
/// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
/// </summary>
/// <param name="function">数据绑定方法</param>
public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) {
System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
SetPageIndex(_grid, e.CommandName);
if (dataBind != null) {
dataBind();
} else {
_grid.DataBind();
}
}
/**//// <summary>
/// 增加首页、上页、下页、尾页按钮
/// </summary>
public static void RowCreated(object sender, GridViewRowEventArgs e) {
RowCreated(sender, e, null);
}
/**//// <summary>
/// 增加首页、上页、下页、尾页按钮
/// </summary>
/// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>
public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) {
if(e.Row.RowType == DataControlRowType.Pager) {
System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
if(_grid.PagerTemplate == null) {
_grid.PagerSettings.Mode = PagerButtons.Numeric;
int pageIndex = _grid.PageIndex;
int pageCount = _grid.PageCount;
TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
TableCell objCell;
string[] button = {"首页","上页","下页","尾页"};
if(buttonText != null) {
for(int i = 0; i < buttonText.Length; i++) {
button[i] = buttonText[i];
}
}
bool enabled = true;
enabled = pageIndex > 0 ? true : false;
// 增加首页、上一页按钮
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled));
objRow.Cells.AddAt(0, objCell);
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled));
objRow.Cells.AddAt(1, objCell);
enabled = pageIndex < (pageCount - 1) ? true : false;
// 增加尾页、下一页按钮
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled));
objRow.Cells.Add(objCell);
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled));
objRow.Cells.Add(objCell);
}
}
}
/**//// <summary>
/// 设置GridView的页索引
/// </summary>
/// <param name="command">被单击的按钮的命令</param>
private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
switch(command) {
case "Page$First":
gridView.PageIndex = 0;
break;
case "Page$Prev":
if(gridView.PageIndex > 0)
gridView.PageIndex -= 1;
break;
case "Page$Next":
if(gridView.PageIndex < (gridView.PageCount - 1))
gridView.PageIndex += 1;
break;
case "Page$Last":
gridView.PageIndex = gridView.PageCount - 1;
break;
default:
return;
}
}
/**//// <summary>
/// 一个新的LinkButton对象
/// </summary>
/// <param name="text">文本</param>
/// <param name="cmd">相应命令</param>
/// <param name="css">Css风格</param>
/// <param name="enabled">可用</param>
/// <returns>LinkButton</returns>
private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) {
LinkButton button = new LinkButton();
button.Text = text;
button.CommandName = cmd;
button.CssClass = css;
button.Enabled = enabled;
button.CausesValidation = false;
return button;
}
}
}
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lyout.Web.Extension {
/**//// <summary>
/// GridView 数据绑定委托
/// </summary>
public delegate void GridViewDataBind();
/**//// <summary>
/// 增强 GridView 的功能
/// </summary>
public sealed class GridView {
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
RegisterEvents(gridView, null, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
RegisterEvents(gridView, dataBind, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="pageText">翻页按钮上的文字</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
RegisterEvents(gridView, dataBind, pageText, true);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
RegisterEvents(gridView, dataBind, null, autoChangePage);
}
/**//// <summary>
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
/// </summary>
/// <param name="gridView">需要注册事件的GridView控件</param>
/// <param name="dataBind">数据绑定方法</param>
/// <param name="pageText">翻页按钮上的文字</param>
/// <param name="autoChangePage">是否自动添加点击页码的相应事件</param>
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText, bool autoChangePage) {
gridView.RowCommand += delegate(object sender, GridViewCommandEventArgs e) {
RowCommand(sender, e, dataBind);
};
gridView.RowCreated += delegate(object sender, GridViewRowEventArgs e) {
RowCreated(sender, e, pageText);
};
if (autoChangePage) {
gridView.PageIndexChanging += delegate(object sender, GridViewPageEventArgs e) {
gridView.PageIndex = e.NewPageIndex;
if (dataBind != null) {
dataBind();
} else {
gridView.DataBind();
}
};
}
}
/**//// <summary>
/// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
/// </summary>
public static void RowCommand(object sender, GridViewCommandEventArgs e) {
RowCommand(sender, e, null);
}
/**//// <summary>
/// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
/// </summary>
/// <param name="function">数据绑定方法</param>
public static void RowCommand(object sender, GridViewCommandEventArgs e, GridViewDataBind dataBind) {
System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
SetPageIndex(_grid, e.CommandName);
if (dataBind != null) {
dataBind();
} else {
_grid.DataBind();
}
}
/**//// <summary>
/// 增加首页、上页、下页、尾页按钮
/// </summary>
public static void RowCreated(object sender, GridViewRowEventArgs e) {
RowCreated(sender, e, null);
}
/**//// <summary>
/// 增加首页、上页、下页、尾页按钮
/// </summary>
/// <param name="buttonText">按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页</param>
public static void RowCreated(object sender, GridViewRowEventArgs e,string[] buttonText) {
if(e.Row.RowType == DataControlRowType.Pager) {
System.Web.UI.WebControls.GridView _grid = (System.Web.UI.WebControls.GridView)sender;
if(_grid.PagerTemplate == null) {
_grid.PagerSettings.Mode = PagerButtons.Numeric;
int pageIndex = _grid.PageIndex;
int pageCount = _grid.PageCount;
TableRow objRow = (TableRow)e.Row.Cells[0].Controls[0].Controls[0];
TableCell objCell;
string[] button = {"首页","上页","下页","尾页"};
if(buttonText != null) {
for(int i = 0; i < buttonText.Length; i++) {
button[i] = buttonText[i];
}
}
bool enabled = true;
enabled = pageIndex > 0 ? true : false;
// 增加首页、上一页按钮
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[0], "Page$First", "", enabled));
objRow.Cells.AddAt(0, objCell);
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[1], "Page$Prev", "", enabled));
objRow.Cells.AddAt(1, objCell);
enabled = pageIndex < (pageCount - 1) ? true : false;
// 增加尾页、下一页按钮
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[2], "Page$Next", "", enabled));
objRow.Cells.Add(objCell);
objCell = new TableCell();
objCell.Controls.Add(LinkButton(button[3], "Page$Last", "", enabled));
objRow.Cells.Add(objCell);
}
}
}
/**//// <summary>
/// 设置GridView的页索引
/// </summary>
/// <param name="command">被单击的按钮的命令</param>
private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
switch(command) {
case "Page$First":
gridView.PageIndex = 0;
break;
case "Page$Prev":
if(gridView.PageIndex > 0)
gridView.PageIndex -= 1;
break;
case "Page$Next":
if(gridView.PageIndex < (gridView.PageCount - 1))
gridView.PageIndex += 1;
break;
case "Page$Last":
gridView.PageIndex = gridView.PageCount - 1;
break;
default:
return;
}
}
/**//// <summary>
/// 一个新的LinkButton对象
/// </summary>
/// <param name="text">文本</param>
/// <param name="cmd">相应命令</param>
/// <param name="css">Css风格</param>
/// <param name="enabled">可用</param>
/// <returns>LinkButton</returns>
private static LinkButton LinkButton(string text, string cmd, string css, bool enabled) {
LinkButton button = new LinkButton();
button.Text = text;
button.CommandName = cmd;
button.CssClass = css;
button.Enabled = enabled;
button.CausesValidation = false;
return button;
}
}
}
首先非常感谢各位对我上篇文章的关注,小弟更新迟了,在此道歉。
上一次发布的代码在使用中还是有一些缺点,比如没有导出数据的功能等。
这次增加导出功能,可导出Excel、Word、HTML等格式。再加入了可隐藏指定列的功能。此类已经自动设置指定的GridView的AllowPaging为True了,更简化用户的操作。
说明:由于很多兄弟的导出功能需要设置几个地方才可以用,本次发布的代码加入了 ExportDataPage 类,只要调用GridView的页面继承该类,就不用任何设置就可以使用导出功能,具体请看演示。
这次加上了演示站点,代码不方便粘贴,请下载使用,谢谢。
代码下载 源码里附有演示。