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

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.