Solved

How to set session variable in Javascript

Posted on 2011-02-28
11
746 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 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…

706 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

21 Experts available now in Live!

Get 1:1 Help Now