关于gridview获取当前行信息的这个古老的话题。

据说在以前的datagrid中,这个问题很好解决。可是发展到gridview,情况却变得复杂了。真是一代不如一代。

方法一:直接利用commandname="select"属性设置,将当前字段设置为选择按钮的功能,然后调用gridview的选中行的信息。

方法二:当gridview中某行需要多个linkbutton时,总不能为每个linkbutton都设置为select吧?可以如下: 

  protected   void  LinkButton2_Click( object  sender, EventArgs e)
    {
        
int  index  =   0 ;
        LinkButton lb 
=  (LinkButton)sender;
        
for  ( int  i  =   0 ; i  <  GV_SoftwareList.Rows.Count; i ++
        {
            
if  ((LinkButton)(GV_SoftwareList.Rows[i].FindControl( " LinkButton2 " ))  ==  lb)
            {
                index 
=  i;
                
break ;
            }
        }
        
string  url  =   " SoftwareDetail.aspx?ComputerName= "   +  ((LinkButton)(GV_SoftwareList.Rows[index].Cells[ 0 ].FindControl( " LinkButton1 " ))).Text.ToString();
        
string  jscode  =   string .Format( " <script>window.open('{0}')</script> " , url);
        Response.Write(jscode);
    }

方法三:http://baikaiyun.blog.hexun.com/7654112_d.html  这哥们挺聪明的。

文章来源: http://blog.csdn.net/lijigang1982/archive/2006/09/19/1244158.aspx

.NET2.0提供了一个GridView控件,这个控件为我们带来了许多的方便,但其去掉了datagird中通过事件按钮访问某行数据的便捷方式(e.item.itemindex),这样给访问GridView中获取当前行的数据增加了麻烦。在我做项目开发过程中,我也碰到了类似的问题,我先在网上搜索了下,找到如下相关帖子:
http://blog.csdn.net/jresins/archive/2005/11/20/533533.aspx,此帖给出了一种解决方案,其关键是要设置CommandArgument参数,如果此参数方便设置,应该是种好的解决方案。但是CommandArgument参数很难决定又如何呢?
其实我们可以变通的利用GridView已经提供的事件方法,在实际中,我就是利用了GridView提供的Delete事件方法实现了对数据行的访问。具体的思路是在利用GridView的GridView1_RowDeleting事件,其两个参数object sender和GridViewDeleteEventArgs e,其中后者可以取得当前操作的行。这样,我们可以通过如下代码this.GridView1.Rows[e.RowIndex].Cells[x].Text取得行中的数据。
具体怎样设置呢?其实很简单,就是给GridView控件增加个模板列,编辑此模板列,拖放Button控件进去,指定其CommandName为“Delete”,然后给GridView增加个事件:GridView1_RowDeleting,在此事件里获取相应的行的数据。
注意,我们并不是要对当前行做删除操作,在GridView1_RowDeleting事件的尾部,我们加上如下代码: e.Cancel = true
就是这样处理。经过我的实践,在运行过程中没有出现什么错误!
下面所示是我项目开发中的部分代码:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
       //获取选择文档的版本,名称,由名称获取其ID,
       docedition = this.GridView1.Rows[e.RowIndex].Cells[2].Text.Trim();
       docname = this.GridView1.Rows[e.RowIndex].Cells[1].Text.Trim();
       ……
       e.Cancel = true;
}

方法四:http://blog.csdn.net/jresins/archive/2005/11/20/533533.aspx

 

.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
-----------------------------------
以下连接有提到另一种方法,但我没有试成功.

.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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值