19 Jan 2012

Pass Send GridView Row Value/Data Using Hyperlink in ASP.NET

In this example i am going to describe how to pass transfer or send GridView data or values from GridView row to Other asp.net page using hyperlink in GridView.

I've put a hyperlink column in gridview to pass values through querystring, and using request.querystring on the second page to retrieve values.

You would also like to read 
LinkButton in GridView and QueryString in ASP.NET to pass data










We need to set DataNavigateUrlFields and DataNavigateUrlFormatString properties of hyperlink in gridview to pass the row data  

HTML markup of the page look like
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID,Name,Location" 
DataNavigateUrlFormatString=
"Default2.aspx?id={0}&name={1}&loc={2}" 
Text="Transfer values to other page" />
<asp:BoundField DataField="ID" HeaderText="ID" 
                SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" 
                SortExpression="Name" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]">
</asp:SqlDataSource>

Now write code mentioned below to retrieve values on Default2.aspx page 
C# code behind
protected void Page_Load(object sender, EventArgs e)
    {
        string strID = Request.QueryString["id"];
        string strName = Request.QueryString["name"];
        string strLocation = Request.QueryString["loc"];
        lblID.Text = strID;
        lblName.Text = strName;
        lblLocation.Text = strLocation;
    
VB.NET code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim strID As String = Request.QueryString("id")
    Dim strName As String = Request.QueryString("name")
    Dim strLocation As String = Request.QueryString("loc")
    lblID.Text = strID
    lblName.Text = strName
    lblLocation.Text = strLocation
End Sub

Hope this helps

3 comments:

  1. Excellent example. I've managed to solve my problem with it. Cheers

    ReplyDelete
  2. Spent two days trying to work this out, followed your example and sorted it out in 10 minutes. Many Many thanks Patel_PC

    ReplyDelete
  3. Very Good Example.

    ReplyDelete