代碼
在伺服器端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);
}
}