Can I access session variable in JavaScript? [Resolved]

Posted by Programmer123 under JavaScript on 9/26/2013 | Points: 10 | Views : 114646 | Status : [Member] | Replies : 4
Can I access session variable in JavaScript?




Responses

Posted by: Sheonarayan on: 9/26/2013 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Yes, you can. Do following


<script type="text/javascript">
var yourVariable = '<%= Session["SessionKey"] %>';
</script>


Now, you can use yourVariable in the page wherever you want.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/26/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
With in script tags use the following
var SesVar = '<%= Session("MySessionVariable").ToString() %>';



Sample: How to Access Asp.Net Session Variable value in javascript
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;



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Yes, you acccess the session variable in JavaScript.

In code-behind you set Session with some data.

Session["usedData"] = "example data";


In JavaScript:-

var sessionValue = '<%=Session["usedData"]%>'


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER


Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can also see the below links:-

http://forums.asp.net/t/394291.aspx
http://www.coderanch.com/t/603251/JSP/java/Access-session-variable-JavaScript
http://www.codeproject.com/Questions/168207/How-to-access-session-variable-through-JavaScript

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response