Give this a try:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" >
<Columns>
<asp:BoundField DataField="Fighter" HeaderText="Fighter" />
<asp:BoundField DataField="Engine" HeaderText="Engine" />
<asp:BoundField DataField="Thrust" HeaderText="Thrust" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:ImageButton ID="cmdView" runat="server"
ImageUrl = '<%# Eval("ImagePath") %>'
Width="150"
OnClientClick='<%# "myrow(" + Eval("ID").ToString + ");return false" %>'
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script>
function myrow(rindex) {
alert("Clicked row index: " + rindex)
}
</script>
The code to load is as follows:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGridF()
End If
End Sub
Sub LoadGridF()
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand("SELECT * FROM Fighters", conn)
conn.Open()
Dim rstData = New DataTable
rstData.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstData
GridView1.DataBind()
End Using
End Using
End Sub
And here is the output:
https://i.sstatic.net/gThkt.png
However, I often find such expressions a bit messy, so I like to create custom attributes for button clicks (or image button clicks).
For example, you can customize the click event like this:
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:ImageButton ID="cmdView" runat="server"
ImageUrl='<%# Eval("ImagePath") %>'
Width="150"
OnClientClick='myrow(this);return false'
MyRowIndex='<%# Container.DisplayIndex %>'
MyPKID='<%# Eval("ID") %>'
MyFighterName='<%# Eval("Fighter") %>'
/>
</ItemTemplate>
</asp:TemplateField>
By adding these attributes, handling events becomes much simpler. Here's what happens when you click on the image now.
https://i.sstatic.net/kndbp.png
I also commonly apply this approach to server-side buttons.
You can then access the custom attributes like this:
Dim btn As Button = sender
Debug.Print(btn.Attributes("MyFigher").ToString)