Hi khaled2016,
Use LinkButton control and set the Session value on its Click event handler.
Refer below sample and correct your code.
HTML
Default.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnlNavigate" runat="server" OnClick="Redirect" Text='<%#Eval("Country") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Details.aspx
Country : <asp:Label ID="lblCountry" runat="server"></asp:Label>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Redirect(object sender, EventArgs e)
{
LinkButton lnk = (sender) as LinkButton;
GridViewRow row = (GridViewRow)lnk.NamingContainer;
Session["Country"] = (row.FindControl("lnlNavigate") as LinkButton).Text;
Response.Redirect("Details.aspx");
}
Details.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lblCountry.Text = Session["Country"].ToString();
}
}
VB.Net
Default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Redirect(ByVal sender As Object, ByVal e As EventArgs)
Dim lnk As LinkButton = TryCast((sender), LinkButton)
Dim row As GridViewRow = CType(lnk.NamingContainer, GridViewRow)
Session("Country") = (TryCast(row.FindControl("lnlNavigate"), LinkButton)).Text
Response.Redirect("Details.aspx")
End Sub
Details.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblCountry.Text = Session("Country").ToString()
End If
End Sub
Screenshot