0

in my home page i updated the session variable from client side using javascript as per the below:

function test(){
  var myvar = "Hi All"
  '<% Session("temp") = "' + myvar + '"  %>';
  alert('<%=Session("temp") %>');
 }

the alert shows the updated value which is correct "Hi All", but when trying to catch the session variable in the code behind in other page, i got the name of variable "myvar" not the value of this variable:

MessageBox.Show(HttpContext.Current.Session("temp"), "mymessage")

this message box displays: '+ mayvar +'

any idea about this issue??

thank you in advance

  • It cannot be set via Javascript directly. Try checking out this link, it has a possible solution. http://stackoverflow.com/questions/17720916/assign-session-value-using-javascript – BobbyDazzler Mar 05 '14 at 14:14

2 Answers2

2

You are mixing client side scripting (javascript) with server side scripting (ASP.NET). This cannot work.

NOTE: your first code DO NOT UPDATE the session variable with the value of the client-side var "myvar". Here is the code generated on the client side:

function test() {
    var myvar = "Hi All"
    '';
    alert('' + myvar + '');
}

As you see alert shows the content of the local variable "myvar", and NOT the content of Session. In order to achieve the result I suggest you to use ajax. Example: jquery setting session in asp.net -> Answer no. 3

Community
  • 1
  • 1
CodeCarvings
  • 224
  • 1
  • 3
  • yea thats what im trying to do, im not sure if its correct or not but my concern is that the value is being updated in JS since the alert shows the updated value and also is updated at server side but is catching the wrong value. what i mean is there any way to send the value in other format in order to read it correctly from vb page code behind – user3139206 Mar 05 '14 at 14:20
1

It is not possible to assign the session variable on the JavaScript side before sending to server side.

There is a great solution to the problem here: http://www.codeproject.com/Questions/341573/JavaScript-How-to-Set-values-to-Session-in-Javascr

The top voted answer should help you!

kylealonius
  • 223
  • 2
  • 9