Solved

How to set session variable in Javascript

Posted on 2011-02-28
11
764 Views
Last Modified: 2012-05-11
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
Comment
Question by:Pralad
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34999551
0
 
LVL 7

Accepted Solution

by:
dxdinh earned 250 total points
ID: 34999557
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34999583
0
 
LVL 1

Author Comment

by:Pralad
ID: 35005828
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Expert Comment

by:leakim971
ID: 35005944
The session variable of the server of something similar on the browser
0
 
LVL 7

Expert Comment

by:dxdinh
ID: 35013407
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
 
LVL 82

Expert Comment

by:leakim971
ID: 35013445
>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
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 35888494
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
 
LVL 7

Expert Comment

by:dxdinh
ID: 35888495
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split in Javascript 5 31
Javascript and Jquery not firing 9 42
Randomize in Owl Carousel v1.3.2 6 20
More SharePoint App with Angular and Bootstrap 17 15
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now