如何在gridview selectedindexchanged期间确定单击了哪个模板列?

时间:2013-08-16 17:41:37

标签: asp.net vb.net gridview

我的gridview上有2个模板列,并希望为每个列执行不同的代码。如何确定单击了哪个模板列?我正在使用下面的代码用于一个模板列,但如果我有另一个我不希望这个代码触发,我想执行不同的代码。感谢

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    Dim row As GridViewRow = DirectCast(DirectCast(sender, ImageButton).NamingContainer, GridViewRow)
    DirectCast(row.NamingContainer, GridView).SelectedIndex = row.RowIndex
    Session("ReferenceNumber") = GridView1.SelectedRow.Cells(3).Text
    Session("ID") = GridView1.SelectedRow.Cells(1).Text
    Response.Redirect("~/frmEditRecord.aspx")
End Sub

2 个答案:

答案 0 :(得分:2)

如果您想要在所选索引中区分所有模板字段列,则更改事件具有ImageButton控件以及CommandName属性,那么您可以在事件处理程序中执行此操作:

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    Dim buttonClicked As ImageButton = TryCast(sender, ImageButton)

    If buttonClicked Is Not Nothing Then
        Dim commandName As String = buttonClicked.CommandName

        If commandName = "Delete" Then
            ' Do something with delete button here
        ElseIf commandName = "Edit" Then
            ' Do something with edit button here
        ElseIf commandName = "Pdf" Then
            ' Do something with PDF button here
        End If
    End If

    Dim row As GridViewRow = DirectCast(DirectCast(sender, ImageButton).NamingContainer, GridViewRow)
    DirectCast(row.NamingContainer, GridView).SelectedIndex = row.RowIndex
    Session("ReferenceNumber") = GridView1.SelectedRow.Cells(3).Text
    Session("ID") = GridView1.SelectedRow.Cells(1).Text
    Response.Redirect("~/frmEditRecord.aspx")
End Sub

答案 1 :(得分:1)

使用CommandName =“select”到templateFields:

<asp:TemplateField HeaderText="Edit">
                 <ItemTemplate >
 <asp:ImageButton ID="ImgSelect"  runat="server" CommandName="select"  ImageUrl="~/Imagesedit_icon.jpg" />