ASP.Net GridView - 如何获取所选列索引(在动态网格视图中)

时间:2014-03-03 11:08:04

标签: asp.net vb.net gridview

我的asp.net页面中有很多动态生成的GridView。在所有GridView中,所有单元格都是链接按钮,它们也是动态生成的。我希望选择单元格/链接按钮索引。

我该怎么做?

注意 - 我没有GridView名称,因为它是在循环中动态生成的。

代码如下 -

Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click      
Call GetFinal()
End Sub

Public Sub GetFinal()
 For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()

'DataTable filling Code goes here

GrdView1.DataSource = dt1

AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub

Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

' All cell to LinkButton code goes here

AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub

1 个答案:

答案 0 :(得分:0)

您创建linkBut​​tons

Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        LinkButton lnkShow= new LinkButton();
        lnkShow.Text = "Show";
        lnkShow.CommandArgument = "Show";
        lnkShow.CommandArgument = e.Row.Cell(0); ' For instance
    End IF
End Sub

然后你处理GridView1.RowCommand事件:

Public Sub GrdView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)        
    If e.CommandName = "show" Then
        LinkButton lnkShow= (LinkButton)sender;
        argument = e.CommandArgument;
        (do something)
    End IF
End Sub

我认为你不需要Link1_Click