3

Work on Asp.Net C# VS08 I would like to assign a value to Session using javascript. Is it possible?

example: Session["Id"] = document.getElementById("id").value;

4 Answers 4

4

The session is a server-side concept; Javascript has no conception of it.

You can make an AJAX service that sets a session value.

However, you should probably use a cookie instead.
You can set cookies in Javascript using this library, among others.

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

Comments

2

No it is not possible to do that directly with JavaScript on the client-side.

Sessions are handled on the server-side. In general your browser would simply store a cookie which uniquely identifies a session stored on your server. When you change the value of a session variable, you are storing this on the server, and the browser cookie would be unaffected.

You could however use JavaScript to send an XMLHttpRequest to your ASP.NET application, which in turn changes the session variable on the server-side. Since this involves a round-trip to the server, you would have to wait for the response, to confirm that the operation succeeded.

Comments

1

Assigning to a session variable like this isn't possible in JavaScript. Best you can do is write data to a cookie.

http://www.w3schools.com/js/js_cookies.asp

Otherwise if you want to communicate to the server use Ajax.

Comments

0

No it is not possible. Session is server side object. A couple of ways to do that would be to send cookies from JavaScript and use them on the server side to assign a value to a session variable, or to make an AJAX call and set the values on the backend.

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.