Can you access Session Variables from Client Side Script?

E

Eliyahu Goldin

No, you have to pass them to client, for example in a hidden input element.

Eliyahu
 
?

=?ISO-8859-1?Q?Anders_Nor=E5s?=

Craig said:
if so, how?
You cannot access Session directly from client side script. However, you
can generate JavaScript code to assign the value of the Session variable
to a client side variable. If you change the client side variable,
you'll have to post the new value back in order to update the Session.

Here is a simple example which creates a JavaScript variable with the
value of a Session variable:
<%@ Page language="c#" AutoEventWireup="true" %>
<script runat="server">
void Page_Load()
{
Session["MySessionVar"]="Hello world";
}
</script>
<html>
<script language="JavaScript">
var mySessionVar="<%= Session["MySessionVar"] %>";
</script>
<body>
<form runat="server"></form>
</body>
</html>

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
C

Craig G

nice one, cheers for the help!

Anders Norås said:
Craig said:
if so, how?
You cannot access Session directly from client side script. However, you
can generate JavaScript code to assign the value of the Session variable
to a client side variable. If you change the client side variable,
you'll have to post the new value back in order to update the Session.

Here is a simple example which creates a JavaScript variable with the
value of a Session variable:
<%@ Page language="c#" AutoEventWireup="true" %>
<script runat="server">
void Page_Load()
{
Session["MySessionVar"]="Hello world";
}
</script>
<html>
<script language="JavaScript">
var mySessionVar="<%= Session["MySessionVar"] %>";
</script>
<body>
<form runat="server"></form>
</body>
</html>

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top