AjaxPro.HttpSessionStateRequirement.ReadWrite
关于在ASP.NET如何使用AjaxPro,这里就不多说了,先看代码:
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] publicstring btnNextZX(string data) { StringBuilder zx_Ret =new StringBuilder(); Hashtable hash = (Hashtable)Session["jgd_data"];
注意:这里排除Session中没有["jgd_data"]这个数据而引起的出错,当然即使不存在也报的并不是如下的错误: 很奇怪!我明明加了如下代码啊!!
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
解决办法:
将如下代码行:
Hashtable hash = (Hashtable)Session["jgd_data"];
替换成如下代码:
Hashtable hash = (Hashtable)HttpContext.Current.Session["jgd_data"];
执行OK!!
找到原因了:Session以及Application 、Request等,都是由Page类继承下来的(你可以this.Session点出来),而用Ajax.net对后台方法访问的时候,这些方法并不属于Page类,所以才需要从HttpContext.Current中读取。而Session又更特别些,需要加[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]才能被访问。
使用AjaxPro时遇到一个问题: 利用AjaxPro调用后台代码,出现错误,由于后台代码与Session有交互
错误信息为: "只有在配置文件或Page指令中将enableSessionState设置为true时,才能使用会话状态.还请确保在应用程序配置的\\节中包括System.Web.SessionStateMod或自定义会话状态模块"
解决方法如下,在Web.config中允许Session,并在ajaxpro方法前加 [AjaxMethod(HttpSessionStateRequirement.ReadWrite)] // 读写Session: 或 [AjaxMethod(HttpSessionStateRequirement.Read)] // 只读Session:
错误信息为: "只有在配置文件或Page指令中将enableSessionState设置为true时,才能使用会话状态.还请确保在应用程序配置的\\节中包括System.Web.SessionStateMod或自定义会话状态模块"
解决方法如下,在Web.config中允许Session,并在ajaxpro方法前加 [AjaxMethod(HttpSessionStateRequirement.ReadWrite)] // 读写Session: 或 [AjaxMethod(HttpSessionStateRequirement.Read)] // 只读Session:
【服务器端】 // Page_Load protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(test)); this.Button_Write.Attributes.Add("onclick","WriteSession();"); //写Session this.Button_Read.Attributes.Add("onclick", "ReadSession();"); //读Session }
// 写入Session(注意参数AjaxPro.HttpSessionStateRequirement.ReadWrite) [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] public void WriteSession(string str) { Session["UserName"] = str; }
// 读出Session(注意参数AjaxPro.HttpSessionStateRequirement.ReadWrite) [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] public string ReadSession() { string str = ""; if (Session["UserName"] != null) { str = Session["UserName"].ToString(); } return str; }
【客户端】 // 写入Session function WriteSession() { var str = "HAHA"; test.WriteSession(str,CallBack_WriteSession); }
function CallBack_WriteSession(res) { if(res.error == null) { alert("OK"); } else { alert(res.error.message); } }
// 读出Session function ReadSession() { test.ReadSession(CallBack_ReadSession); }
function CallBack_ReadSession(res) { if(res.error == null) { alert(res.value); } else { alert(res.error.message); } }
// 写入Session(注意参数AjaxPro.HttpSessionStateRequirement.ReadWrite) [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] public void WriteSession(string str) { Session["UserName"] = str; }
// 读出Session(注意参数AjaxPro.HttpSessionStateRequirement.ReadWrite) [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)] public string ReadSession() { string str = ""; if (Session["UserName"] != null) { str = Session["UserName"].ToString(); } return str; }
【客户端】 // 写入Session function WriteSession() { var str = "HAHA"; test.WriteSession(str,CallBack_WriteSession); }
function CallBack_WriteSession(res) { if(res.error == null) { alert("OK"); } else { alert(res.error.message); } }
// 读出Session function ReadSession() { test.ReadSession(CallBack_ReadSession); }
function CallBack_ReadSession(res) { if(res.error == null) { alert(res.value); } else { alert(res.error.message); } }