获取GridView行索引 Ver2

前一版本http://www.cnblogs.com/insus/archive/2013/01/13/2858768.html 是用Javascript获取GridView的行索引。

此篇Insus.NET使用非Javascript获取GridView的行索引。

数据还是使用前一篇的数据来更改。

首先为GridView的控件写OnRowCreated ="GridViewCosmetic_RowCreated"

 1  Protected Sub GridViewCosmetic_RowCreated(sender As Object, e As GridViewRowEventArgs)
 2         If e.Row.RowType = DataControlRowType.DataRow Then
 3             If e.Row.FindControl("LinkButton1") IsNot Nothing Then
 4                 Dim link As LinkButton = DirectCast(e.Row.FindControl("LinkButton1"), LinkButton)
 5                 AddHandler link.Click, AddressOf link_click
 6             End If
 7         End If
 8     End Sub
 9 
10     Private Sub link_click(sender As Object, e As EventArgs)
11         Dim link As LinkButton = DirectCast(sender, LinkButton)
12         Dim gvr As GridViewRow = DirectCast(link.Parent.Parent, GridViewRow)
13 
14         Response.Write("<scr" & "ipt>alert('你选择的行索引是:" & gvr.RowIndex & "')</scr" & "ipt>")
15     End Sub

 

posted @ 2013-01-13 20:24  Insus.NET  阅读(722)  评论(0编辑  收藏  举报