Accessing Session information from Javascript

  • Thread starter Jeronimo Bertran
  • Start date
J

Jeronimo Bertran

Hi,

How can I access session information stored during the code behind from
within my javascript?

Example... in my javascript I have the following:

var url = "RenderPage.aspx";
document.all.MyFrame.contentWindow.navigate(url);


Where MyFrame is an IFrame..... inside the codebehind for RendePage.aspx I
am setting a session variable:

Session["MyVar"] = "hello";


I then want to be able to use this information from my javascript to fill a
field in my main form:



document.MyForm.elements[2].value = Session("MyVar"); // This doesn't
work.

Thanks a million,

Jeronimo
 
G

Guest

Hi,

Client-Side scripts cannot access Server-Side variables! Hence you cannot
access the server-side session object from your javascript.

I'd suggest that instead of savig that value to a session save it to a
hidden input field, so that the JavaScript can read the value from it.
 
J

Jeronimo Bertran

ok, now... my problem is that the value that needs to be saved is generated
inside a page that lies inside a frame of the form that needs to know the
value.... how can I save the value to a hidden input field in the main
form? Or access the hidden input field in the child frame from my main
form?
 
G

Guest

Another option could be to have a hidden iframe and navigate to a page (via
javascript) that returns the session variable value (for instance, something
getSessionValue.aspx?var=varName).

The getSessionValue.aspx only needs to do the something like
Response.Write(Session(Request.QueryString["var"]))

Then, you can access via Javascript to inner value of that div

This option gives you session information at the moment, but you have to
deal with syncronization issues and javascript client programing.

Regards,
Leon
 
Joined
Jun 17, 2010
Messages
1
Reaction score
0
Accessing server side session in javascript

<script type="text/javascript">

Var test= '<%= Session("gs_UserID") %>';

</script>


where Session("gs_UserID") is a server side session variable
 

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,781
Messages
2,569,615
Members
45,293
Latest member
Hue Tran

Latest Threads

Top