Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
4.50/5 (4 votes)
i want to assign a value to a session from javascript function

<%Session["Test"] = "Welcome Mamu";%> 



the above code will work properly. but i want to assign a value from a variable like

var strTest=document.getElementById('DropDownList1').value;
<%Session["Test"] = "'+ strTest +'";%>



this is woking but the value saved in the session is '+ strTest +' not the value of strTest

is anybody know the solution plz reply me soon
Posted
Updated 17-Apr-11 22:33pm
v2

Of course the valuue stored in the seesion is '+ strTest +' because that is what you assigned to Session["Test"].
What you're trying to achieve will not work this way since the code inside the <% %> is executed on the serverside during rendering of the page and the var strTest = ... bits are javascript are code that is run on the client side (browser). If you want to set the session variable you'll either have to submit the data including that of DropDownList1 or do an AJAX call to your website if the value of the DropDownList1 changes.

Best Regards,

-MRB
 
Share this answer
 
v2
I think you misunderstood his question. He wants to access the Session variable through JavaScript which is impossible. This is purely due to that session is encrypted, mainly to guard it off from hackers. Just think, if it was accessible from JavaScript, how many websites will be hacked in a blink :)

But depending on what you want, you might be able to workaround it. A simple method I would think of is, use AJAX to call a server-side method to fetch the value you want from the session. But please, keep it in mind that this is NOT RECOMMENDED. A method that you can call, anyone else can call :) Unless, you authenticate the access.

My advice to you is to use a hidden field in your page to carry the values that you want to process using javascript.

Hope this helps.

Regards, Pasan.
 
Share this answer
 
Comments
dhage.prashant01 18-Apr-11 5:59am    
Good1
Manfred Rudolf Bihy 18-Apr-11 6:56am    
Your comment to my solution has miraculously vanished, but your solution propagates exactly what I told OP. So I don't really see any difference in what you are expressing.
Ankur\m/ 18-Apr-11 7:03am    
Good answer, 5 to you!
Prasanta_Prince 18-Apr-11 9:12am    
Good answer.
BobJanova 18-Apr-11 11:00am    
Hidden fields are also accessible to everyone, and a bit harder to secure than an AJAX call.
You fundamentally misunderstand what the session is. The session is stored on the server and is used to persist data between page requests from the same user. It is not available on the client side (i.e. in the browser) because the browser is showing one page.

It does not make sense to set session variables in response to client actions, as client actions are occuring within the context of one page and do not need to be persisted between requests.

If you want to take a record of a user's choice, you should submit that choice, either by AJAX or a form submission (depending on the circumstances; AJAX for single values, form submissions for a whole set at once). The server page that responds to that submission might choose to store some information in the session, but it might not.
 
Share this answer
 

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