views:

254

answers:

2

Hi All,

How to store values in the session using jquery. I am using the following code

var Link = '<%=Session["Link"]%>';

to get data from session. How to do the reverse process.

Problem:

 I need this in my master page. so i cant use .ajax{}.

Geetha

+1  A: 

You will probably need to set up an HTTP handler or something similar that will accept requests and store things in the Session. Visual studio has this somewhere in the "Add" menus in the solution view. (If you're using ASP.NET MVC, you just set up another action instead of a generic handler.)

Since those values will may have different types (int, string, etc.) and since you don't want malicious users poking into any session key they see fit, you will probably want to set up a branch to determine what to do with each key. Something like this (inside the handler that you created):

string key = context.Request.QueryString["key"].ToString();
string val = context.Request.QueryString["val"].ToString();
if(key == "AshDiffuserID"){
  int ash_diffuser_id = Convert.ToInt32(val);
  Session["AshDiffuserID"] = ash_diffuser_id;
}
else if(key == "PesterchumHandle") {
  string handle = val;
  Session["PesterchumHandle"] = handle;
} else // etc...

After that, you'll need to set up a post HTTP request through jquery that puts whatever values you need in those "key" and "val" fields.

$.post(
  'url/to/your/handler.ashx',
  {key: "PesterchumHandle", val: "carcinoGenetecist"}
); 
Jesse Millikan
A: 

You will need to do an ajax post to the server via jquery.

thekaido