ie421.NET

面对技术,你别无选择,.NET世界是如此精彩,而我们要做的就是:Thinking More

博客园 首页 新随笔 联系 订阅 管理

===后台代码====

 

public partial class SessionWebForm : System.Web.UI.Page
{
    [AjaxPro.AjaxMethod]
    
public static bool SetSession(string name, string value)
    
{
        HttpContext.Current.Session[name] 
= value;
        
return true;
    }


    [AjaxPro.AjaxMethod]
    
public static string GetSession(string name)
    
{
        
return HttpContext.Current.Session[name].ToString();
    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        AjaxPro.Utility.RegisterTypeForAjax(
typeof(SessionWebForm));
    }

}

 

====前台=====

 

<!-- content -->

<p>If you want to access the session collection you have to use the HttpContext instead of the page property.</p>

<pre class="codeSample">public partial class SessionWebForm : System.Web.UI.Page
{
  [AjaxPro.AjaxMethod]
  public static bool SetSession(string name, string value)
  
{
    HttpContext.Current.Session[name] 
= value;
    
return true;
  }


  [AjaxPro.AjaxMethod]
  public static string GetSession(string name)
  
{
    
return HttpContext.Current.Session[name].ToString();
  }


  protected 
void Page_Load(object sender, EventArgs e)
  
{
    AjaxPro.Utility.RegisterTypeForAjax(
typeof(SessionWebForm));
  }

}
</pre>

<p>Click <a href="javascript:setSession();void(0);">here</a> to set a session value and <a href="javascript:getSession();void(0);">here</a> to get the value back from server-side code. Try to reload this page (using Internet Explorer press F5) and click on the second link to see if you get the same value!</p>

<script type="text/javascript">

function setSession() {
    SessionWebForm.SetSession(
"test"new Date().toLocaleString(), function(res){
        
if(res.error != null{
            alert(res.error.Message);
            
return;
        }

        alert(
"Session value is set to '" + res.request.args.value + "'");
    }
);
}


function getSession() {
    SessionWebForm.GetSession(
"test"function(res){
        
if(res.error != null{
            alert(res.error.Message);
            
return;
        }

        alert(
"Session value = " + res.value);
    }
);
}


</script>

<!-- end of content -->
posted on 2008-07-27 13:00  ie421  阅读(226)  评论(0编辑  收藏  举报