AjaxPro使用方法【转】

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);


这样就能在js和cs中同时使用UserName这个属性了,当然修改也要提供2套方案。 原因请看推荐的那篇文章,我就不啰嗦了。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值