0

我的 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
4

2 回答 2

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
于 2013-08-16T17:58:41.490 回答
1

使用 CommandName = "select" 到 templateFields:

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

于 2013-08-16T18:32:02.070 回答