温故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>
 
复制代码

 

服务器端代码如下:

 

复制代码
代码
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;
        }
    }
}
复制代码

 

 

由于代码简单,我就不多说了。
posted @   路途遥远  阅读(1053)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示