分享:Ajax读写Session值
上午搞了一上午,为了实现ajax验证用户的权限,权限保存在session中。一直提示错误,一不小心在网上找到答案,原来很简单。大家分享下!
在服务器端page_load
AjaxPro.Utility.RegisterTypeForAjax(typeof(test));
this.Button_Write.Attributes.Add( "onclick ", "WriteSession(); ");//写session
this.Button_Read.Attributes.Add( "onclick ", "ReadSession(); ");//读session
其他写和读的方法
/// <summary>
/// 写session
/// </summary>
/// <param name= "str "> </param>
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void WriteSession(string str)
{
Session[ "UserName "] = str;
}
/// <summary>
/// 读session
/// </summary>
/// <returns> </returns>
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public string ReadSession()
{
string str = " ";
if (Session[ "UserName "] != null)
{
str = Session[ "UserName "].ToString();
}
return str;
}
客户端代码:
//4、访问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);
}
}
//访问
function ReadSession()
{
test.ReadSession(CallBack_ReadSession);
}
function CallBack_ReadSession(res)
{
if(res.error == null)
{
alert(res.value);
}
else
{
alert(res.error.message);
}
}
原址:http://blog.csdn.net/01022417/archive/2006/10/17/1337527.aspx
------解决方案--------------------SF
马克。