[学习笔记][ASP.NET2.0][GridView][在按钮事件中访问GridView中当前行数据]

.NET2.0提供了一个GridView控件,这个控件为我们带来了许多的方便,但其去掉了datagird中通过事件按钮访问某行数据的便捷方式(e.item.itemindex).

通过某些变通的方式,我们仍然能够达到我们的目的:

我在网上找到一篇文章,原文如下:

I've been working on a project recently that involves using the new GridView in ASP.NET 2.0 a fair amount. In general, I'm quite pleased with this new grid implementation - it feels more intuitive, more extensible, and lighter weight than the monstrosity that is the DataGrid.
There's one thing that's bugging me, however, since I can't find a simple way to do it and it is such a common thing in Web programming that I feel I must be overlooking something: to add a command column to the grid, the handler of which uses the current row of data to perform some task. The most common thing is to have a hidden primary key field which you then access through the Cells array of the current row (at least this was the technique in 1.1 with the DataGrid).
There is a nice ButtonField that you can add to your columns that looks like it should do the trick:
 
< asp : ButtonField CommandName ="use" HeaderText ="Use" Text ="Use" />
 
then you can add a handler for the RowCommand event of the GridView and look for the "use" command name (this seems analogous to the ButtonColumn/ItemCommand pair in the DataGrid).
 
  protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e)
  {
    if (e.CommandName == "use")
    {
    }
  }
 
So far so good. The first problem is that the GridViewCommandEventArgs has no reference to the current row (you could access it in the old DataGrid's DataGridCommandEventArgs.Item property). Ok, no problem - with a little spelunking you can use the CommandSource property and walk up to its grandparent and that turns out to the be the row - not the prettiest code, but at least we have the row.
 
GridViewRow row = ( GridViewRow )(( Control )e.CommandSource).Parent.Parent;
 
Now, I try and grab the value of the hidden ID column at index 0 (again, a common technique with the DataGrid).
 
string id = row.Cells[0].Text;
 
But the cell has no text in it! After some spelunking in the debugger, the value of the hidden primary key column does not seem to be there at all. As you can see, it feels like I'm swimming upstream here, and that I must be missing some new feature in the GridView that provides this same functionality. If anyone knows what that is, please post a comment.
 
If you have the same problem and are looking for a solution, here's what I ended up doing - just using a template field with a LinkButton and passing the ID in the CommandArgument field:
 
< asp : TemplateField >
  < ItemTemplate >
    < asp : LinkButton ID ="useLinkButton"
                         CommandArgument =' <%# Eval("id") %> ' Text ="Use"
                         runat ="server" OnCommand ="useLinkButton_Command" />
  </ ItemTemplate >
</ asp : TemplateField >
 
protected void useLinkButton_Command( object sender, CommandEventArgs e)
{
  string id = ( string )e.CommandArgument;
  // do task with id here
}
---------------------------------
原文地址:http://pluralsight.com/blogs/fritz/archive/2005/06/24/11975.aspx
--------------------------------
另外原文也提到一ASP.NET论坛上的一篇帖子:http://forums.asp.net/897693/ShowPost.aspx
-----------------------------------
以下连接有提到另一种方法,但我没有试成功.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值