Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
In a HTML file, I set a session variable's value using JavaScrip (in which the session.js is loaded from http://dreamerslab.com/blog/en/javascript-session/"):
C#
  var ar = ['ID', 'pID', 'cID', 'name', 'type', 'access', 'desc', 'comment', 'lat', 'lon', 'contractName'];
  var monument = Session.get("monument") || {
      rec: []
  };
...
Session.set("monument", ar);

Then, clicking a button to direct to another aspx page. In this aspx's code-behind, I tried to retrieve the session's value,
C#
var sValue = Session["monument"];

but got
C#
Session["monument"]
not defined.
How can this session's value be retrieved? Thanks.

What I have tried:

How to get Session variable value on another aspx page?
Posted
Updated 21-Jun-16 8:29am
Comments
ZurdoDev 20-Jun-16 12:10pm    
var sValue = Session["monument"]; is the right way to get it. This means it is not being set.
Sergey Alexandrovich Kryukov 20-Jun-16 12:18pm    
Right. It could be just the matter of the order of operations. It it is not defined, it should not be a problem.
—SA
s yu 20-Jun-16 12:30pm    
Before I click the HTML button to re-direct to the aspx, I retrieve the value by Session.get("monument") in the Immediate Window. It does display exactly the array values. Then, the Page_Load() of the aspx, Session["monument"] is null. Any additional advisory to figure out the reason? Thanks.
ZurdoDev 20-Jun-16 12:39pm    
Session.get("monument") is in JavaScript so I have no idea what it does nor do I trust it.

How are you re-directing to the other page?
s yu 20-Jun-16 13:44pm    
Using an alternative approach, now I can pass the session value to another page. The description is below:
1) In 1st aspx, create a hidden variable.
2) In the 1st aspx, set a session variable's value;
3) In a button click event (in JavaScript), Set the hidden variable = session variable's value.
4) After 3), fire document.getElementById('<%=Button2.ClientID%>').click();
5) In the code-behind: Button2_Click(object sender, EventArgs e) {
Session["monument"] = HiddenField.Value;
Response.Redirect("2.aspx");
}
6) When 2.aspx is open, the Session["monument"]'s value can then be retrieved.
Thanks for your review and response.

1 solution

To get? You probably can. But to set? You can't, JavasSript is used for client side scripting on the browser, and cannot access a Session object from a server. JavaScript runs in the clients browser, your Session object is exposed by the server. There's disconnected space between them.

However, you can connect JavaScript to a web service, and inside the web service you can return data from your source to the JavasSript method.

If you're lazy you could store the value in a HiddenField instead so you can easily access it in your JavaScript code. Or use the dirty way like this to get the value:

JavaScript
<script type="text/javascript">
    var sessionValue = '<%= Session["Test"] %>';
    alert(sessionValue); 
</script>


The <%= variable %> is a databinding expression that binds server-side variable value to client. Note that that will only work if you are passing a value (get a value from server). It won't work for setting value from your JavaScript. That's why I still prefer using an AJAX call or a HiddenField.
 
Share this answer
 
v2

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