Storing values in asp.net session using jquery

非 Y 不嫁゛ 提交于 2019-12-10 23:54:12

问题


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:


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"}
); 



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/2332082/storing-values-in-asp-net-session-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!