There are two common ways to get the ‘id’ associated with a particular GridView. One is to actually retrieve it, with the ID field being visible in the Gridview. But one way is to make a label INVISIBLE, and bind it to the ID field of the data.
This code sample uses and shows both. Notice that the SelectCommand for the DataSourceID are in the Page_Load event. This is to cut down on page real estate, due to the size of the SQL statement.
The ‘Get’ column retrieves the ID from the visible column in the datagrid (the label in the TemplateField, bound to the ID). The ‘Other’ column retrieves the ID from the label inside the first column that has it’s Visible property set to ‘false’.
<script language="VB" Runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
SQLDS1.SelectCommand="SELECT Products.ProductID, Products.ProductName, Categories.CategoryID," & _
"Categories.CategoryName, Suppliers.CompanyName FROM Products INNER JOIN Categories ON " & _
"Products.CategoryID = Categories.CategoryID INNER JOIN Suppliers ON " & _
"Products.SupplierID = Suppliers.SupplierID"
End Sub
Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs)
Dim gvr As GridViewRow
gvr = CType(sender.parent.parent, GridViewRow)
Dim lbl As Label = CType(gvr.FindControl("lblID"), Label)
lblResults.Text = lbl.Text
End Sub
Sub IB_Hidden(sender As Object, e As System.Web.UI.ImageClickEventArgs)
Dim gvr As GridViewRow
gvr = CType(sender.parent.parent, GridViewRow)
Dim lbl2 As Label = CType(gvr.FindControl("lblOther"), Label)
lblResults.Text = lbl2.Text & "/Other"
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Getting An ID of a GridViewRow</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:GridView Runat="server"
Id="MyGridView"
GridLines="Both"
cellpadding="0"
cellspacing="0"
BorderWidth="1"
AutoGenerateColumns="False"
Headerstyle-BackColor="#BDCFE7"
Headerstyle-Forecolor="#000000"
Headerstyle-Font-Names="Arial"
Headerstyle-Font-Bold="True"
Headerstyle-Font-Size="10"
BackColor="#E7EFFF"
Font-Names="Arial"
Font-Size="8"
AlternatingRowStyle-BackColor="#FBFBFB"
AlternatingRowStyle-Font-Names="Arial"
AlternatingRowStyle-Font-Size="8"
BorderColor="Black"
AllowSorting = "True"
AllowPaging = "True"
PageSize = "10"
PagerSettings-Mode = "Numeric"
DataSourceID="SQLDS1">
<Columns>
<asp:TemplateField HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text=''<%# Bind("ProductID") %>''></asp:Label>
<asp:Label ID="lblOther" Visible="False" runat="server" Text=''<%# Bind("ProductID") %>''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:TemplateField HeaderText="Get">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/update.gif"
OnClick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Other">
<ItemTemplate>
<asp:ImageButton ID="ibHidden" runat="server" ImageUrl="images/update.gif"
OnClick="IB_Hidden" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblResults" Font-Bold="True" Font-Size="14pt" Runat="server" />
<asp:SqlDataSource ID="SQLDS1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>">
</asp:SqlDataSource>
</form>
</body>
</html>