Solved

How to set session variable in Javascript

Posted on 2011-02-28
11
814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

728 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