Sachu
Sachu

Reputation: 7766

How to use session variable in URL through ASPX page

I have an aspx page There is a hyperlink to another page with some parameters. The parameters to pass is stored in Session variables.

I tried like below

<a href="Home.aspx?C="+<%= Session("id").ToString()%>>Home</a>

The name 'Session' does not exist in the current context

Edited

I changed () to []

Home

Now no error but when clicking the link the page is coming but the URL is

Home.aspx?C=

The session value is not displaying

I am getting below error. Is it not possible to access the session variable like above in aspx page?

Upvotes: 0

Views: 6332

Answers (2)

David
David

Reputation: 218827

This URL:

<a href="Home.aspx?C="+<%= Session["id"].ToString()%>>Home</a>

Would produce something like this:

<a href="Home.aspx?C="+someValue>Home</a>

Which is invalid markup. The value should be inside the double-quotes, and shouldn't include that +:

<a href="Home.aspx?C=<%= Session["id"].ToString()%>">Home</a>

This would produce something like:

<a href="Home.aspx?C=someValue">Home</a>

Upvotes: 2

Shaminder Singh
Shaminder Singh

Reputation: 1293

Use the below code

<a href='<%= "Home.aspx?C="+Session["id"].ToString %>'>Link</a>

Upvotes: 0

Related Questions