.NET下的简单AJAX处理库

代码下载:ajax.rar

说明:

可以实现的任务
————————
   请求页                                     执行过程                                              用于处理结果的目标
—————                       ——————————————                              ———————————
页面A请求 ->                               Ajax任务                                             ->             A中的元素或一个js脚本
页面A请求 ->            Ajax任务->模板页以及模板页js、模板页后台       ->     A中的元素或一个js脚本
………………                                 ………………                                                 ………………
………………                                ………………                                                  ………………

使用指南
————
1.  配置:需要在Web.config中配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <httpHandlers>
  <add verb="*" path="Dispatcher.ajax" type="ViewLogic.Ajax.AjaxDispatcher,程序集名称"/>
      </httpHandlers>
    </system.web>
</configuration>

然后在IIS信息管理里添加扩展名ajax: 管理工具->Internet 信息服务->默认网站->主目录->配置(G)里点添加,然后
可执行文件添 C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/aspnet_isapi.dll
扩展名: .ajax
检查文件是否存在: 不选


2.  前台:程序的前台部分引用了Prototype 1.5,它可以帮助我们完成所有AJAX客户端的任务和其他客户端任务。
   其架构请参考压缩包的 "prototype15.png" 文件。
   需要依赖的包:

   <script src="prototype.js"></script>
   使用Ajax对象的语法:
          <script>
            new Ajax.Updater(element,url,{Constructors});
          </script>

           element:        Ajax请求返回的结果给哪个元素。
           url:                要请求的地址,本库固定使用Dispatcher.ajax为Ajax统一传入地址
           Constructors:  构造器,由此传入其他可选值。在本Ajax库内:Constructor必须包含evalScripts: true
   主要构造器参数为:
       pars: 要传入的参数
       method: 传输的方法(get,post)

   在本Ajax库中,需要传入三种参数,以便处理:

    ·command(必添) : 具体要执行的Ajax任务, 不区分大小写.
     例如: 想执行AjaxCheckUser类的任务,则command=CheckUser;
    ·callback(可选)   : 指定Ajax任务执行完是否要执行前台的指定回调函数.
                            例如: 执行完Ajax任务后想把结果alert出来,则设置callback=doCallback;
                                    然后实现回调方法
                                        function doCallback(val) {
                                            alert(val);
                                        }
   ·toUrl(可选)       : 指定Ajax任务的模板页. 用法: toUrl=[url].
    例如: 执行完Ajax任务, 要以a.aspx为模板显示结果, 则toUrl=a.aspx
     
   callback和toUrl可以叠加使用.
 例如:

  a) pars = "command=FindForward&callback=gotoPage";
     var myAjax = new Ajax.Updater('placeholder',"Dispatcher.ajax",{method: 'get',parameters: pars,evalScripts: true});
  b) pars = "command=lookdetail&callback=initdiv&toUrl=Ajax/lookdetail.aspx"
     var myAjax = new Ajax.Updater({success: 'placeholder'},"Dispatcher.ajax",{method: 'post',parameters: pars,evalScripts: true});
 
   其他有关Prototype的具体使用方法请参考同级目录下的 "Prototype使用手册.htm"
         
2.  后台:
   后台需在指定的命名空间ViewLogic.Ajax.AjaxObject内实现具体AJAX任务策略.
   示例请参考    /后台/AjaxObject/AjaxCheckUser.cs,  /后台/AjaxObject/AjaxHash.cs
          AJAX任务策略类必须继承AjaxBase类, 必须重写DoAjax()方法.
          输出格式有三种:  Output();                               // 用于把自定义信息传到模板页,自定义信息须放入context.Items属性中
                                  Output(string str);                  // 用于传单个文本到指定目标
                                  Output(IDictionary dictionary); // 用于传一个或多个对象到指定目标
   如果需要其他格式请另行添加.

3.  模板页:
   设置toUrl参数后, 模板页功能才有效
   模板页须使用Web窗体(.aspx和.aspx.cs)文件, 用context.Items来获取传入数据,
   如果后台使用Output(string str); 方式传值,则使用context.Items["AjaxOutput"]取得.
   模板页可以进行后台处理.

注意:AjaxObjectFactory里的类装载器Activator.CreateInstance的第一个参数为程序集名称,使用时应更改

举例一: 页面A请求 ->   Ajax任务  ->     A中的一个js脚本方式   来传输多个值
前台:
<html>
        <head>
             <script src="prototype.js"></script>
        </head>
               
     <body>
        <script>
              new Ajax.Updater('','Dispatcher.ajax?command=hash&callback=test',{evalScripts: true});
              function test(hash) {
                     alert(hash["name"]) ;
             }
       </script>
     </body>
</html>
  callback=test定义执行完要调用test函数
  command=hash定义请求应该传到AjaxHash.cs

后台:
using System;
using System.Collections;
namespace ViewLogic.Ajax.AjaxObject
{
public class AjaxHash : AjaxBase
{
  protected override void DoAjax()
  {
   Hashtable hash = new Hashtable(5);
   hash.Add("a",1);
   hash.Add("b",2);
   hash.Add("name",”haha“);
   hash.Add("d",4);
   hash.Add("e",5);
   Output(hash);
  }
}
}

最后执行结果为 haha.

举例二:页面A请求 ->  Ajax任务 -> 模板页后台       ->     A中的元素   来传输多个值
前台:
<html>
        <head>             <script src="prototype.js"></script>
        </head>
               
     <body>
        <div id=aa>dddddddddddddddd</div>
        <a href="#" onclick = "onclk()">click</a>
        <script>
             function onclk() {
              new Ajax.Updater('aa','Dispatcher.ajax?userid=haha&userval=55&command=test&toUrl=test.aspx',{evalScripts: true});
             }
       </script>
     </body>
</html>
  command=test定义请求应该传到AjaxTest.cs
toUrl=test.aspx 定义模板页是test.aspx

后台
using System;
using System.Collections;
namespace ViewLogic.Ajax.AjaxObject
{
public class AjaxHash : AjaxBase
{
  protected override void DoAjax()
  {
   string userid = request.Params["userid"];
   string userval = request.Params["userval"];
   Hashtable hash = new Hashtable(3);
   hash.Add("userid",userid );
   hash.Add("userval ",userval );
   hash.Add("name","123");
   Output(hash);
  }
}
}
模板页 前台:
……
<asp: Label id="a" runat="server"></asp: Label>
<asp: Label id="b" runat="server"></asp: Label>
<asp: Label id="name" runat="server"></asp: Label>
……
模板页后台:
private void Page_Load(object sender, System.EventArgs e) {
     a.Text = Context.Items["userid"].ToString();
     b.Text = Context.Items["userval"].ToString();
     name.Text = Context.Items["name"].ToString();
}

最后执行结果为 在链接click上单击,则aa层由dddddddddddddddd变为  haha 55 123

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值