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

2:17:00 am 0 Comments

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


<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
1protected void Page_Load(object sender, EventArgs e)
2    {
3        string strID = Request.QueryString["id"];
4        string strName = Request.QueryString["name"];
5        string strLocation = Request.QueryString["loc"];
6        lblID.Text = strID;
7        lblName.Text = strName;
8        lblLocation.Text = strLocation;
9    }
VB.NET code behind
1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
2    Dim strID As String = Request.QueryString("id")
3    Dim strName As String = Request.QueryString("name")
4    Dim strLocation As String = Request.QueryString("loc")
5    lblID.Text = strID
6    lblName.Text = strName
7    lblLocation.Text = strLocation
8End Sub

Hope this helps

0 comments: