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;
            }
        }
    }
    由于代码简单,我就不多说了。
  • 相关阅读:
    google git的使用方法
    C/C++ 开发库 | C/C++ Development Library
    log4cplus c++开源日志系统
    c++配置类
    Markdown基础语法
    Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。
    Nhibernate refers to an unmapped class nhibernate问题的解决(初学者)
    UICollectionView的使用
    Runloop
    UITableView(转)
  • 原文地址:https://www.cnblogs.com/luoguoqiang1985/p/1674868.html
Copyright © 2011-2022 走看看