Storing values in asp.net session using jquery
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{}.
开发者_Python百科Geetha
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"}
);
I wasted my whole day to sort this problem while this is just a quick fix. During PageMethod call, session id is not being passed with request URL, so new session_start event is getting fired. We just need to set exact request path before calling a pagemethod, so that new session start event can not be fired.
if ('<%= HttpContext.Current.Session.IsCookieless %>==True') {
//need to pass session id in request path
PageMethods.set_path('<%= System.Web.Configuration.WebConfigurationManager.AppSettings("WebRoot") %>(S(<%=Session.SessionID%>))/secure/PageName.aspx');
}
PageMethods.PageMethodName(param1,param2, CallSuccess, CallFailed);
You will need to do an ajax post to the server via jquery.
精彩评论