Rahul Soni's blog

Never assume the obvious is true!

GridView:Access GridView's Row Items information from the Button Click Event in ASP.NET 2.0

GridView:Access GridView's Row Items information from the Button Click Event in ASP.NET 2.0

  • Comments 8

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.

Attachment: GridView1.zip
  • Good Post
  • 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

    ~ Rahul
  • Nice... but what if you don't want to display the product ID???  Even if you include the column and set it's visible property to false it doesn't return the value.  I'm looking to do something very similar to this, except all of my ID's are GUID which don't look so hot in a grid.  What are my options??  Thanks.

  • 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.

  • 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!!!

  • How can I call the RowEditing event of the GridView on the click of external button.

  • Check this out

    http://www.codeproject.com/useritems/Hide_GridView_Column_Cell.asp

  • I have to set EnableEventValidation="false" for run above code .

Page 1 of 1 (8 items)
Leave a Comment
  • Please add 5 and 8 and type the answer here:
  • Post