[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 877
  • Last Modified:

How to set session variable in Javascript

Hi,

I wish to assign session variable from javascript. I am able to do by means of below code
'<%Session["Key"] = "' + Key + '"; %>';
at aspx page value assigned to Session["Key"] is the variable value "Key"

but when trying to access from .aspx.cs (code behind) getting session["key"] values are  ' + Key + '. and looking to retrieve actual value of variable Key instead of " ' + Key + ' ", how could this be achieved.

Please suggest.

Thanks


0
Pralad
Asked:
Pralad
1 Solution
 
dxdinhCommented:
Session variable can only be set at server side code - You can not assign session from javascript - it will be a major security violation for sure - Imagine what hackers can do to server through javascript

I suggest to pass the variable back from javascript to server using querystring form

or using postback hidden field.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
PraladAuthor Commented:
Thanks Leakim971.

But, I am looking for assigning value to session variable through javascript. All the above examples are related to retrieval. Please provide method to assing vlaue to session variable in JS without Webservice,etc.

0
 
leakim971PluritechnicianCommented:
The session variable of the server of something similar on the browser
0
 
dxdinhCommented:
I can not stress this enough - you can not set session variable using client side javascript. You have to pass to the server by the mean of using query string or hidden field post back.

Perhaps, you can tell us what you want to achieve then we can help you to achieve the same result without setting the session variable through client side javascript (it's just not possible).

0
 
leakim971PluritechnicianCommented:
>The session variable or the server or something similar on the browser ?

Using PageMethods  (check the Page_Loads section) : http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2
0
 
Amandeep Singh BhullarCommented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
dxdinhCommented:
You can not set session via client script - it's against security - if everyone can set session variable via javascript on client side - they can easily take down your server.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now