Click here to Skip to main content
15,745,014 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi I have a session variable on my page and I am setting a value to it on .cs page.

I want to access this value on other page using javascript.
I went further in following manner:
var someSession = '<%= HttpContext.Current.Session["USERID"] %>';
alert(someSession );

But I am not getting the value of session....instead I am getting
<%=HttpContext.Current.Session["USERID"] %> in the alert box.

How can I approach further?

You can simply set the session value to an asp:HiddenField and access that hidden field value in javascripts.

C# code

HiddenField1.Value = Session["keyName"];

In javascripts, access that value like below

var sessionVal = document.getElementById('<%=HiddenField1.ClientID%>').value;

or you can try this way as well

var username = '<%= Session["UserName"] %>';
alert(username );

Praveen Nelge

[edit]Code block fixed[/edit]
Share this answer
aks.shan 18-Feb-14 6:14am    
I am working on .xsl page and there is no code behind for this page.
So I don't think hidden field will serve the purpose.
[no name] 18-Feb-14 6:16am    
Have you checked both the ways which I have mentioned above ?

aks.shan 18-Feb-14 6:21am    
Yes I have checked both the ways.
I wont be able to use hidden field because the hidden field on xsl page doesnot have access to .cs page.
[no name] 18-Feb-14 6:28am    
I am not telling to you use hidden fields please check it properly
This is the alternative way which I have specified.

var username = '<%= Session["UserName"] %>';
alert(username );

aks.shan 18-Feb-14 6:30am    
I have already tried this option...I am getting the <%= Session["UserName"] %> in alert window and not the value of session.
Thanks guys for your valuable help.
Just had to use a hidden field variable on xsl page and then assign it value in code behind page from session by creating a xmlnode.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900