温故AjaxPro系列之四(读取和设置Session)
AjaxPro服务器端程序如果想读取或设置Session的值,特性需要指定为:Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite[Read|Write]).下面我将以实际程序演示怎么使用。
第一步我们新建SessionRw页面。页面代码如下:
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionRw.aspx.cs" Inherits="AjaxProDemo.SessionRw" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="Js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function SetSession(SeVal)
{
var val = $("#Text1").val();
SessionRw.SetSeesion(val, SetCb);
return;
}
function SetCb(rep)
{
if (rep.error != null)
{
alert(rep.error);
}
else
{
if (rep.value = '1')
alert("设置成功!");
else
alert("设置失败!");
}
return;
}
function GetSession()
{
SessionRw.GetSeesion(Get_Cb);
return;
}
function Get_Cb(rep)
{
if (rep.error != null)
{
alert(rep.error);
}
else
{
$("#div1").attr("innerText", rep.value);
}
return;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
读写session测试
</div>
<br />
设置Seesion:
<input id="Text1" type="text" />
<input id="Button1" type="button" value="设置" onclick="SetSession();"/>
<input id="Button2" type="button" value="读取Session" onclick="GetSession();"/>
<br />
Session:<div id="div1"></div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="Js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function SetSession(SeVal)
{
var val = $("#Text1").val();
SessionRw.SetSeesion(val, SetCb);
return;
}
function SetCb(rep)
{
if (rep.error != null)
{
alert(rep.error);
}
else
{
if (rep.value = '1')
alert("设置成功!");
else
alert("设置失败!");
}
return;
}
function GetSession()
{
SessionRw.GetSeesion(Get_Cb);
return;
}
function Get_Cb(rep)
{
if (rep.error != null)
{
alert(rep.error);
}
else
{
$("#div1").attr("innerText", rep.value);
}
return;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
读写session测试
</div>
<br />
设置Seesion:
<input id="Text1" type="text" />
<input id="Button1" type="button" value="设置" onclick="SetSession();"/>
<input id="Button2" type="button" value="读取Session" onclick="GetSession();"/>
<br />
Session:<div id="div1"></div>
</form>
</body>
</html>
服务器端代码如下:
代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ajax;
namespace AjaxProDemo
{
public partial class SessionRw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(SessionRw));
}
[Ajax.AjaxMethod(HttpSessionStateRequirement.Read)]
public string GetSeesion()
{
return HttpContext.Current.Session["Session"] == null ? string.Empty : HttpContext.Current.Session["Session"].ToString();
}
[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
public bool SetSeesion(string val)
{
bool ret = true;
try
{
HttpContext.Current.Session["Session"] = val;
}
catch(Exception e)
{
ret = false;
}
return ret;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ajax;
namespace AjaxProDemo
{
public partial class SessionRw : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(SessionRw));
}
[Ajax.AjaxMethod(HttpSessionStateRequirement.Read)]
public string GetSeesion()
{
return HttpContext.Current.Session["Session"] == null ? string.Empty : HttpContext.Current.Session["Session"].ToString();
}
[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
public bool SetSeesion(string val)
{
bool ret = true;
try
{
HttpContext.Current.Session["Session"] = val;
}
catch(Exception e)
{
ret = false;
}
return ret;
}
}
}
由于代码简单,我就不多说了。