GridView:Access GridView's Row Items information from the Button Click Event in ASP.NET 2.0
Requirement 1
=============
You have a GridView in ASP.NET 2.0 and you want to read the row whenever the Button of that row is clicked. Additionaly, you want to pass the key information to a different page, where you are supposed to show the details.
Download the attached project and find all the necessary files to run this project. Here, I will just point out the main stuff...
In the web.config file we need to add the following information about the Connection String
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
In the Default.aspx file add the following between <div> and </div> tags
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:TemplateField HeaderText="Show Details">
<ItemTemplate>
<asp:Button ID="btnDetails" runat="server" OnClick="Details_Click" Text="Detail" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [QuantityPerUnit] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
The Default.aspx.vb file looks as follows...
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Details_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'
Dim gridRow As GridViewRow
Dim strID, strName As String
'
gridRow = CType(sender, Button).Parent.Parent
strID = gridRow.Cells(0).Text
strName = gridRow.Cells(1).Text
Session.Add("ID", strID)
Session.Add("Name", strName)
Response.Redirect("Details.aspx", True)
End Sub
End Class
The Details.aspx.vb file looks as follows...
Partial Class Details
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'
If Not Session.Item("ID") Is Nothing Then
Response.Write("ID = " & Session.Item("ID"))
End If
Response.Write("<BR>")
If Not Session.Item("Name") Is Nothing Then
Response.Write("Name = " & Session.Item("Name"))
End If
End Sub
End Class
Now, run the project with Default.aspx as the start page. When you click on anyof the details button, you will be able to read the row information and pass it on to the other page.
Comments
Anonymous
August 07, 2006
Good PostAnonymous
September 05, 2006
Hey here is something that allows dynamic download that means, donload on button click instead of providing direct link.
http://ankitjain.info/ankit/2006/07/01/forcing-download-on-web-page
~ RahulAnonymous
November 30, 2006
The comment has been removedAnonymous
November 30, 2006
Hmmm, it seems I have managed to solve my own problem. It's weird. If you hide the column at design time the product ID will always be blank. If you hide the column one cell at a time using the RowDataBound event everything is peachy: Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound e.Row.Cells(0).Visible = False End Sub BUT, if you hide the entire column at once in the DataBound event, it breaks again: Protected Sub gv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound GridView1.Columns(0).Visible = False End Sub ...I don't like that.Anonymous
November 30, 2006
Honestly speaking, that doesn't sound good to me either. I am quite busy these days, but I will sure look into it whenever I get a chance! Thanks bunch for commenting though!!!Anonymous
December 17, 2006
How can I call the RowEditing event of the GridView on the click of external button.Anonymous
August 27, 2007
Check this out http://www.codeproject.com/useritems/Hide_GridView_Column_Cell.aspAnonymous
June 09, 2009
I have to set EnableEventValidation="false" for run above code .