All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Visual Studio 2005中Crystal Reports数据源列举
PHP中多张图片上传并校验的实现_[PHP教程]
在ASP.NET中web.config配置数据库_[Asp.Net教程]
php上传文件功能函数_php资料_编程技术
包含实时功能的ASP.NET系统结构_.net资料_编程技术
PHP入门需要掌握的几种功能代码_[PHP教程]
PHP中的session的几个问题_php资料_编程技术
asp.net,URL重写(URLRewriter),之简化版_.net资料_编程技术
C#中break语句的使用方法
Asp.Net中虚拟文件系统的使用_[Asp.Net教程]
Reading number is top 10 pictures
NeedWallpaper11
Summer is most suitable for young people to travel in China4
Sell the barbecue as says father du breul5
治疗多发性骨髓瘤的特效药,一万二一支
西游四格漫画(六)
全球清廉国家排行
2012 national geographic daily picture6
传奇套装
So beauty, will let you spray blood10
到南昌西站了1
Download software ranking
Desire a peach blossom
软件工程思想
Sora aoi 120 minutes
Sora aoi‘s film--Lust fan wall
株洲本地在线棋牌游戏
asp.netWeb服务器高级编程
传奇私服架设教程
Tram sex maniac 2 (H) rar bag11
Tram sex maniac 2 (H) rar bag18
The cock of the Grosvenor LTD handsome
delv published in(发表于) 2014/1/23 3:11:22 Edit(编辑)
ASP.NET实例:增强,GridView,控件的功能_[Asp.Net教程]

ASP.NET实例:增强,GridView,控件的功能_[Asp.Net教程]

ASP.NET实例:增强 GridView 控件的功能_[Asp.Net教程]

还记得雅虎的XX(不好意思,忘记姓甚名谁了)有一篇演讲,其中一个字就是“懒”。咱写代码的,不懒点儿还真是吃不消,尤其是现在这样要命的天气~~。


相信大家都多少有抱怨GridView控件的功能吧,咱在这就不多说了,贴出代码是最重要的。



该类增强了微软的GridView的功能,增加了“首页”、“上页”、“下页”、“尾页”按钮,方便大家使用,翻页事件也已经添加好,在相关页面只要添加以下代码就可以了:


protected void Page_Init(object sender, EventArgs e) {
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 {
/**////


/// GridView 数据绑定委托
///

public delegate void GridViewDataBind();


/**////


/// 增强 GridView 的功能
///

public sealed class GridView {
/**////
/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
///

/// 需要注册事件的GridView控件
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView) {
RegisterEvents(gridView, null, true);
}


/**////


/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
///

/// 需要注册事件的GridView控件
/// 数据绑定方法
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind) {
RegisterEvents(gridView, dataBind, true);
}


/**////


/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
///

/// 需要注册事件的GridView控件
/// 数据绑定方法
/// 翻页按钮上的文字
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, string[] pageText) {
RegisterEvents(gridView, dataBind, pageText, true);
}


/**////


/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
///

/// 需要注册事件的GridView控件
/// 数据绑定方法
/// 是否自动添加点击页码的相应事件
public static void RegisterEvents(System.Web.UI.WebControls.GridView gridView, GridViewDataBind dataBind, bool autoChangePage) {
RegisterEvents(gridView, dataBind, null, autoChangePage);
}


/**////


/// 给指定的GridView控件添加RowCommand、RowCreated、PageIndexChanged事件
///

/// 需要注册事件的GridView控件
/// 数据绑定方法
/// 翻页按钮上的文字
/// 是否自动添加点击页码的相应事件
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();
}
};
}
}


/**////


/// 点击每行触发的命令,自动绑定。用于自定义数据绑定方法写在if(!Page.IsPostBack){}外。
///

public static void RowCommand(object sender, GridViewCommandEventArgs e) {
RowCommand(sender, e, null);
}


/**////


/// 点击每行触发的命令,需提供数据绑定方法。用于自定义数据绑定方法写在if(!Page.IsPostBack){}内。
///

/// 数据绑定方法
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();
}
}


/**////


/// 增加首页、上页、下页、尾页按钮
///

public static void RowCreated(object sender, GridViewRowEventArgs e) {
RowCreated(sender, e, null);
}


/**////


/// 增加首页、上页、下页、尾页按钮
///

/// 按钮上的文字。索引:0 首页 1 上页 2 下页 3 尾页
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], "PageFirst", "", enabled));
objRow.Cells.AddAt(0, objCell);


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


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


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


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


/**////


/// 设置GridView的页索引
///

/// 被单击的按钮的命令
private static void SetPageIndex(System.Web.UI.WebControls.GridView gridView, string command) {
switch(command) {
case "PageFirst":
gridView.PageIndex = 0;
break;
case "PagePrev":
if(gridView.PageIndex > 0)
gridView.PageIndex -= 1;
break;
case "PageNext":
if(gridView.PageIndex < (gridView.PageCount - 1))
gridView.PageIndex += 1;
break;
case "PageLast":
gridView.PageIndex = gridView.PageCount - 1;
break;
default:
return;
}
}


/**////


/// 一个新的LinkButton对象
///

/// 文本
/// 相应命令
/// Css风格
/// 可用
/// LinkButton
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;
}
}
}


来源:http://www.cnblogs.com/lyout/archive/2007/06/02/768970.html







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.