AjaxPro 操作Session

 

原文:http://blog.csdn.net/mathew3625/archive/2007/06/12/1648797.aspx

 

1 在web.config中的<system.web>节点中添加:

 

     < httpHandlers >
      
< add verb = " POST,GET " path = " ajaxpro/*.ashx " type = " AjaxPro.AjaxHandlerFactory, AjaxPro " />
    
</ httpHandlers >


2 后台代码(本来想直接写在页面上,后来想,还是保持住良好的习惯吧,我忍了)

         private void Page_Load( object sender, System.EventArgs e)
        
{
                AjaxPro.Utility.RegisterTypeForAjax(
typeof (WebForm1));
            }


            [AjaxPro.AjaxMethod()]
        
public string TestMehtod( string name)
        
{
            
return this .BuildMessage(name);
            }


        
protected string BuildMessage( string name)
        
{
            
return string .Format( " Hello {0}!Welcome to AjaxPro's world. " ,name);
            }


3 前台代码

<% @ Page language = " c# " Codebehind = " SampleBase.aspx.cs " AutoEventWireup = " false " Inherits = " AjaxProSample.WebForm1 " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.0 Transitional//EN " >

< html >
  
< head >
    
< title > WebForm1 </ title >
    
< meta name = " GENERATOR " Content = " Microsoft Visual Studio .NET 7.1 " >
    
< meta name = " CODE_LANGUAGE " Content = " C# " >
    
< meta name = vs_defaultClientScript content = " JavaScript " >
    
< meta name = vs_targetSchema content = " http://schemas.microsoft.com/intellisense/ie5 " >
    
< script language = " javascript " type = " text/javascript " >
        
            function TestFunction()
        
{
                var serverMessage
= AjaxProSample.WebForm1.TestMehtod( ' jxh ' );
            
return serverMessage.value;
            }

        
    
</ script >
  
</ head >
  
< body >
    
< form id = " Form1 " method = " post " runat = " server " ></ form >
    
    
< script language = " javascript " >
            document.write(TestFunction());        
    
</ script >
    
  
</ body >
</ html >

 

 

基本功能实现,下一步当然是更进一步的实践了。接着做的例子是个简单的登录操作,并将用户名和密码用Session记录下来。代码和上面的类似,就不贴了,占地方,呵呵。
但有2点需要注意
1 关于Session,如果想在AjaxMethod中使用Session的话,那么AjaxMethod标签必须带AjaxPro.HttpSessionStateRequirement.ReadWrite参数

 

[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        
public string CheckPassword()
        
{}

 

2 关于属性,我们注册了一个属性,运行之后,在客户端JS中的确就可以访问了

 

            [AjaxPro.AjaxProperty()]
        
public string UserName
        
{
            
set
            
{
                    Session[
" UserName " ] = value;
                }


            
get
            
{
                
return Session[ " UserName " ].ToString();
                }

            }

 


 

AjaxProSample.SampleSession.UserName = document.getElementById( " txtUserName " ).value;

alert(AjaxProSample.SampleSession.UserName);

 


但此时如果在服务器端代码中企图使用的话,就会出现空引用异常,如果非要在客户段和服务器端同时使用这个属性的话,请增加一个设置值的方法。例如:

 

            [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        
public void SetValue( string value)
        
{
                UserName
= value;
            }

 


 

                AjaxProSample.SampleSession.UserName = document.getElementById( " TextBox1 " ).value;
                AjaxProSample.SampleSession.SetValue(AjaxProSample.SampleSession.UserName);

你可能感兴趣的:(session)