zoukankan      html  css  js  c++  java
  • 温故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;
            }
        }
    }
    由于代码简单,我就不多说了。
  • 相关阅读:
    Python实现杨辉三角
    第8.30节 重写Python __setattr__方法实现属性修改捕获
    转:Cookie详解
    第8.29节 使用MethodType将Python __setattr__定义的实例方法与实例绑定
    第8.28节 Python中使用__setattr__定义实例变量和实例方法
    第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
    第8.26节 重写Python类中的__getattribute__方法实现实例属性访问捕获
    关于open函数文件打开模式的有意思的一个现象
    第8.25节 Python风格的__getattribute__属性访问方法语法释义及使用
    转:关于Python中的lambda,这篇阅读量10万+的文章可能是你见过的最完整的讲解
  • 原文地址:https://www.cnblogs.com/luoguoqiang1985/p/1674868.html
Copyright © 2011-2022 走看看