果4点儿

我边学边干,在IT世界闯荡.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET 2.0 GridView RowCommand 事件取得行号

Posted on 2009-04-11 18:04  果4点儿  阅读(4468)  评论(2编辑  收藏  举报

在WebControl中,不像WinForm那样,可以那么容易的取得行号的

因为Web提交的过程中,Grid都不存在了,对于服务器上执行的后台代码当然找不着所谓的e.RowIndex什么的了

用GridView默认的ButtonField  我找了很多资料,也不知道应该如何取得行号,好像说要在行绑定过程中将行号绑定到

它的CommandArguments中,可以在2.0中,我就没发现ButtonField有什么CommandArguments

所以只有用模板列了,用他的例子如下:

aspx中GridView里如下:

  <asp:TemplateField HeaderText="取消发货" >
                        <ItemTemplate> 
                        <asp:Button runat="server" Text="取消" CommandName="CancelSend" />          
                        </ItemTemplate>
 </asp:TemplateField>

不用任何绑定,在后台如下读取行号

C#:

   if (e.CommandName == "CancelSend")
            {
                GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));

//CommandSource 引起事件的命令源
                int RowIndex = drv.RowIndex;               
            }

做到这里就觉得奇怪,为什么用Grid本身的ButtonField不能转化

用ButtonField读的时候,CommandSource 为GridView本身,真是奇怪。这样一来不知道那ButtonField有什么用。

 

还有另一种在命令参数中绑定的方法,可以实现,但感觉没这种方式直接,此处不介绍。