session variables, accessing from client side java script

R

ray well

hi,

is a session varibale acessible for reading and writing from client side
javascript?

say i have a session variable called 'Session("Color")', and i want to
read/write to it, how would i refere to it say in a onclick event?

i would appreciate a snipet of code.

thanks

ray
 
M

Marina

No, it is not accessible from client side javascript. Session information is
stored on the server.
 
R

recoil

If you wish to do so then you need to store the information in
something that is sent to the client side. The easiest way to do this
is to use an <input type="hidden" id="hdfMyColor" runat="server" />
then in your codebehind define the variable as such
or
Protected WithEvents hdfMyColor As
System.Web.UI.HtmlControls.HtmlInputHidden


Attach an event handler to the PreRender method and in there set the
HtmlInputHidden's value to the session value
Private Sub Page_Render(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
hdfMyColor.Value = Session("Color")
End Sub


now you would simply go
<script type="text/javascript">
var colorHolder = document.getElementById("hdfMyColor");
var color = colorHolder.value;
</script>
 
R

recoil

Another method to doing that is to use RegisterClientScriptBlock to set
the variable manually through javascript code.


Private Sub Page_Render(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
RegisterClientScriptBlock("ddd", "<script type='text/javascript'> var
color = '" + Session("Color") + "';</script>")
End Sub


This method may be alot easier
 
W

William F. Robertson, Jr.

No, Session variables reside on the server. If you want access, you will
need to postback to the server for your on click event.

bill
 
W

William F. Robertson, Jr.

How does either of your methods help ray meet his requirements of reading
and writing a session variable?

bill
 
Joined
Jul 31, 2007
Messages
1
Reaction score
0
accessing session value in client side javascript

var color='<%= Session("color").ToString() %>'
this is the simple way to access the session value in javascript
 
Joined
Nov 15, 2008
Messages
1
Reaction score
0
javascript + session

hi,

you can also use hidden field to store the session value and read that value in javascript.

for example,

session["name"]="well";
hiddenctrl.value=session["name"].ToString();
client-side:

javascript:

var sessionvalue= document.getElementById('hiddenctrl').value;

hope it helps
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top