Passing Value Of Grid Row From Data Grid To Webpages Using Hyperlink In ASP.NET

Introduction

In this article, I am going to explain how to pass the value of grid row from data grid of one page to another webpage using hyperlink in ASP.NET in C# and VB.NET.

Sometimes, while working on web technologies, it is necessary to show detailed data of the summary on other web pages. So here, I will show you how you can pass data as input from one webpage to other webpage using hyperlink control.

Implementation

Let's create one small tutorial for a demonstration of this concept.

Here, I will create one data grid in which the grid contains 6 columns, and within the grid, I will make Item Template where first and last columns act as Hyperlink inside the Item Template of Template Field.

HTML

  1. <form id="form1" runat="server">  
  2. <asp:GridView ID="GridView1" HeaderStyle-BackColor="Black" HeaderStyle-ForeColor="White"  
  3.     RowStyle-BackColor="#dddddd" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"  
  4.     runat="server" AutoGenerateColumns="false">  
  5.     <Columns>  
  6.         <asp:TemplateField HeaderText = "Id" ItemStyle-Width="30">  
  7.             <ItemTemplate>  
  8.                 <asp:HyperLink runat="server"   
  9.                     Style="text-decoration:none;font:bold;color:red"   
  10.                     NavigateUrl='<%# Eval("Id", "~/DetailsCS.aspx?Id={0}") %>'  
  11.                     Text='<%# Eval("Id") %>' />  
  12.             </ItemTemplate>  
  13.         </asp:TemplateField>  
  14.         <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" ItemStyle-Width="150" />  
  15.         <asp:BoundField DataField="Company" HeaderText="Company" ItemStyle-Width="150" />  
  16.         <asp:BoundField DataField="Designation" HeaderText="Designation" ItemStyle-Width="150" />  
  17.         <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />  
  18.         <asp:TemplateField>  
  19.             <ItemTemplate>  
  20.                 <asp:HyperLink runat="server"    
  21.                     NavigateUrl='<%# string.Format("~/DetailsCS.aspx?Id={0}&EmployeeName={1}&Company={2}&Designation={3}&Country={4}",   
  22.                     HttpUtility.UrlEncode(Eval("Id").ToString()),   
  23.                     HttpUtility.UrlEncode(Eval("EmployeeName").ToString()),  
  24.                     HttpUtility.UrlEncode(Eval("Company").ToString()),  
  25.                     HttpUtility.UrlEncode(Eval("Designation").ToString()),   
  26.                     HttpUtility.UrlEncode(Eval("Country").ToString())) %>'  
  27.                     Text="Employee Details"    
  28.                     Style="text-decoration:none;font:bold;color:red" />  
  29.             </ItemTemplate>  
  30.         </asp:TemplateField>  
  31.     </Columns>  
  32. </asp:GridView>  
  33.     </form>  

You can see the above-described HTML markup where column Id contains Query String Parameter in NavigateUrl Property of Hyperlink that is called a Single Query String Parameter.

Passing Value Of Grid Row From Data Grid To Webpages Using Hyperlink In ASP.NET 

You can see the above-described HTML markup where column Id, EmployeeName, Designation, Company, and Country contains Query String Parameter in NavigateUrl Property of Hyperlink that is called as Multiple Query String Parameter.

Passing Value Of Grid Row From Data Grid To Webpages Using Hyperlink In ASP.NET 

Here, I have passed the Query String Parameter using Eval functions with the use of String Format function.

Now, I will Create Sample Datatable with some sample record. And I bound that data table with our data grid view.

While we working with data table, first, we need to add its namespace.

C#

using System.Data;

VB.Net

 Imports System.Data

Now, we will start our actual code.

C#

  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!this.IsPostBack)  
  4.         {  
  5.             DataTable dt = new DataTable();  
  6.             dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Id"), new DataColumn("EmployeeName"), new DataColumn("Company"), new DataColumn("Designation"), new DataColumn("Country") });  
  7.             dt.Rows.Add(1, "Nikunj Satasiya""D & K Technology Pvt.Ltd""Software Engineer" ,"India");  
  8.             dt.Rows.Add(2, "Hiren Dobariya""Version Syatem Pvt.Ltd""Web Developer""India");  
  9.             dt.Rows.Add(3, "Pratik Pansuriya""LY Technology Pvt.Ltd""Network Engineer""India");  
  10.             dt.Rows.Add(4, "Vivek Ghadiya""Atos""Software Engineer""India");  
  11.             dt.Rows.Add(5, "Sneha Patel""Atos""Software Engineer""India");  
  12.             dt.Rows.Add(6, "Kishan Patel""Infosys""Application Developer""India");  
  13.             dt.Rows.Add(7, "Sarah Frentco""MasterCard""Software Engineer""India");  
  14.             GridView1.DataSource = dt;  
  15.             GridView1.DataBind();  
  16.         }  
  17.     }  

VB.Net

  1. Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load  
  2.         If Not Me.IsPostBack Then  
  3.             Dim dt As New DataTable()  
  4.             dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("EmployeeName"), New DataColumn("Company"), New DataColumn("Designation"), New DataColumn("Country")})  
  5.             dt.Rows.Add(1, "Nikunj Satasiya""D & K Technology Pvt.Ltd""Software Engineer""India")  
  6.             dt.Rows.Add(2, "Hiren Dobariya""Version Syatem Pvt.Ltd""Web Developer""India")  
  7.             dt.Rows.Add(3, "Pratik Pansuriya""LY Technology Pvt.Ltd""Network Engineer""India")  
  8.             dt.Rows.Add(4, "Vivek Ghadiya""Atos""Software Engineer""India")  
  9.             dt.Rows.Add(5, "Sneha Patel""Atos""Software Engineer""India")  
  10.             dt.Rows.Add(6, "Kishan Patel""Infosys""Application Developer""India")  
  11.             dt.Rows.Add(7, "Sarah Frentco""MasterCard""Software Engineer""India")  
  12.             GridView1.DataSource = dt  
  13.             GridView1.DataBind()  
  14.         End If  
  15.     End Sub  

Output

Passing Value Of Grid Row From Data Grid To Webpages Using Hyperlink In ASP.NET 

Now, it’s time to display Query String Values on the other Webpage at the time of Page Load.

HTML

  1. <form id="form1" runat="server">  
  2. <table>  
  3.     <tr>  
  4.         <td>  
  5.             <b>Id</b>  
  6.         </td>  
  7.         <td>  
  8.             <asp:Label ID="lblId" runat="server"></asp:Label>  
  9.         </td>  
  10.     </tr>  
  11.     <tr>  
  12.         <td>  
  13.             <b>EmployeeName</b>  
  14.         </td>  
  15.         <td>  
  16.             <asp:Label ID="lblEmployeeName" runat="server"></asp:Label>  
  17.         </td>  
  18.     </tr>  
  19.         <tr>  
  20.         <td>  
  21.             <b>Company</b>  
  22.         </td>  
  23.         <td>  
  24.             <asp:Label ID="lblCompany" runat="server"></asp:Label>  
  25.         </td>  
  26.     </tr>  
  27.     <tr>  
  28.         <td>  
  29.             <b>Designation</b>  
  30.         </td>  
  31.         <td>  
  32.             <asp:Label ID="lblDesignation" runat="server"></asp:Label>  
  33.         </td>  
  34.     </tr>  
  35.     <tr>  
  36.         <td>  
  37.             <b>Country</b>  
  38.         </td>  
  39.         <td>  
  40.             <asp:Label ID="lblCountry" runat="server"></asp:Label>  
  41.         </td>  
  42.     </tr>  
  43. </table>  
  44.     </form>  

C#

  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!this.IsPostBack)  
  4.         {  
  5.             lblId.Text = HttpUtility.UrlDecode(Request.QueryString["Id"]);  
  6.             lblEmployeeName.Text = HttpUtility.UrlDecode(Request.QueryString["EmployeeName"]);  
  7.             lblCompany.Text = HttpUtility.UrlDecode(Request.QueryString["Company"]);  
  8.             lblDesignation.Text = HttpUtility.UrlDecode(Request.QueryString["Designation"]);  
  9.             lblCountry.Text = HttpUtility.UrlDecode(Request.QueryString["Country"]);  
  10.         }  
  11.     }  

VB.Net

  1. Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load  
  2.         If Not Me.IsPostBack Then  
  3.             lblId.Text = HttpUtility.UrlDecode(Request.QueryString("Id"))  
  4.             lblEmployeeName.Text = HttpUtility.UrlDecode(Request.QueryString("EmployeeName"))  
  5.             lblCompany.Text = HttpUtility.UrlDecode(Request.QueryString("Company"))  
  6.             lblDesignation.Text = HttpUtility.UrlDecode(Request.QueryString("Designation"))  
  7.             lblCountry.Text = HttpUtility.UrlDecode(Request.QueryString("Country"))  
  8.         End If  
  9.     End Sub  

Output

Passing Value Of Grid Row From Data Grid To Webpages Using Hyperlink In ASP.NET 

Summary

This article explained how you can pass a Value/Parameter from one webpage to another.


Similar Articles
Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.