Pass Session value to HyperLink inside ASP.Net GridView Template Field

Last Reply one year ago By democloud

Posted one year ago

Hello Sir,

I'm trying to make a Gridview column itself as a hyperlink for regenerating reports for that particular row.

I have been using the below code for the same.

<asp:CommandField ShowSelectButton="True" ButtonType="Link" ShowHeader="True" SelectText="Report" ></asp:CommandField>

in Codebehind

 

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = GridView1.SelectedRow.RowIndex;
        clientId = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text);
        string role = obj.Encrypt(Convert.ToString(roleID));
        Response.Redirect("clientreport.aspx?ID=" + role + "&clientId= " + clientId);
    }

Using this I'm able to generate the reports but using my code one new column is getting generated, i want hyperlink to created from where onclick  report should be generated for particular row ID.

Thanks 

 

Results 1 - 5 of 10 12
Posted one year ago

Hello Sir,

Below is the code for passing session variable in aspx page of hyperlink in gridview

public string GetUrl(object registerId, object client_name)
{
    string url ="~/report1.aspx?ID=" + role + "&registerId=" +       Server.UrlEncode(registerId.ToString());
    return url;
}

 

<asp:TemplateField> 
    <ItemTemplate> 
        <asp:HyperLink id="hlDetails2"  Runat="server" Text='<%# Eval("client_name") %>'
        NavigateUrl='<%# GetUrl(Eval("registerId"),Eval("client_name"))%>' /> 
    </ItemTemplate> 
</asp:TemplateField>
   

Thanks

 


Posted one year ago

Hey democloud,

You can't pass session in aspx page of hyperlink.

If you want to pass seesion please refer previous provided sample.


Posted one year ago

Hello Sir,

How to pass session value in below code:

<asp:TemplateField HeaderText="Id" ItemStyle-Width="30">
            <ItemTemplate>
                <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Id", "~/DetailsCS.aspx?Id={0}") %>'
                    Text='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>

 


Posted one year ago

Hey democloud,

Please refer below sample.

Please create rowdatabound event and find hyperlink id and pass values in navigate url..

HTML

CS.aspx

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Id" ItemStyle-Width="30">
            <ItemTemplate>
                <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Id", "~/DetailsCS.aspx?Id={0}") %>'
                    Text='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" Text="View Details" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

DetailsCS.aspx

 

<table>
    <tr>
        <td>
            <b>Id</b>
        </td>
        <td>
            <asp:Label ID="lblId" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            <asp:Label ID="lblName" runat="server"></asp:Label>
        </td>
    </tr>        
</table>

 

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

CS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Session["Id"] = 1;
        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 GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
        hl.NavigateUrl = String.Format("~/DetailsCS.aspx?Id={0}&Name={1}", Session["Id"].ToString(), e.Row.Cells[1].Text);
    }
}

DetailsCS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lblId.Text = Session["Id"].ToString();
        lblName.Text = HttpUtility.UrlDecode(Request.QueryString["Name"]);
    }
}

VB.Net

VB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Session("Id") = 1
        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 GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim hl As HyperLink = CType(e.Row.FindControl("HyperLink2"), HyperLink)
        hl.NavigateUrl = String.Format("~/DetailsCS.aspx?Id={0}&Name={1}", Session("Id").ToString(), e.Row.Cells(1).Text)
    End If
End Sub

DetailsVB.aspx.vb

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        lblId.Text = Session("Id")
        lblName.Text = HttpUtility.UrlDecode(Request.QueryString("Name"))
    End If
End Sub

 Screenshot


Posted one year ago

Hello Sir,

 Getting error in below code "The server tag is not well formed."

<asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("~/client_report.aspx?ID=Session{'roleID'}&clientId={0}",HttpUtility.UrlEncode(Eval("clientId").ToString())) %>'
                    Text="View Details" />
            </ItemTemplate>
        </asp:TemplateField>