您现在的位置: 365建站网 > 365文章 > 使用AjaxPro.Net框架实现在客户端调用服务端方法

使用AjaxPro.Net框架实现在客户端调用服务端方法

文章来源:365jz.com     点击数:297    更新时间:2011-02-19 12:06   参与评论
此文档将使用AjaxPro.Net框架实现Ajax功能:在客户端异步调用服务端方法。AjaxPro.Net是一个优秀的.net环境下的Ajax框架,用法很简单,可以查阅相关资料,本文档是一个简单的实例讲述使用AjaxPro的几个关键点。

1、下载AjaxPro 组件。并将AjaxPro.dll引用到网站(或项目)。下载:Download latest version 7.7.31.1.
2、
修改Web.config。在 <system.web> 元素中添加以下代码。
  <configuration>
<system.web> <httpHandlers> <!-- 注册 ajax handler,2.0以上框架用AjaxPro.2 -->
    <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
  </httpHandlers> </system.web> </configuration>
3、对AjaxPro在页Page_Load事件中进行运行时注册。如:
  //AjaxPro.Utility.RegisterTypeForAjax(typeof(所在类的类名));类的类名。如是放在命名空间,则需要写上完整的命名空间(如:namespaces._Default)
     AjaxPro.Utility.RegisterTypeForAjax(typeof(testPro1));
4、创建服务器端方法。只要给一个方法加上[AjaxPro.AjaxMethod]标记,该方法就变成一个AjaxPro可进行影射调用的方法。如下:(我现在是新建一个testPro1.aspx页面,在它的cs代码中加入)

</>code

  1. [AjaxPro.AjaxMethod]
  2. public string GetString()
  3. {
  4. return "Hello AjaxPro";
  5. }
  6. [AjaxPro.AjaxMethod]
  7. public string GetServerTime()
  8. {
  9. return DateTime.Now.ToString();
  10. }

5、客户端调用:

</>code

  1. <script type="text/javascript">
  2. function getTime() {
  3. alert(testPro1.GetServerTime().value);
  4. }
  5. function getServerStr() {
  6. //ajaxPro_guide.GetString(GetString_callback); // asynchronous call
  7. //var p = ClassPro.GetServerTime().toString();
  8. alert(testPro1.GetString().value);
  9. }
  10. </script>

页面中加入以下代码:
    <input id="Button1" type="button" value="获是服务器时间" onclick="getTime()" />
    <input id="Button3" type="button" value="获是服务器对象" onclick="getStudent()" />

二、扩展,客户端访问服务器对象
  1、在App_code中新建类:

</>code

  1. public class Student
    {
    private string _name = "郑伯城";
    public int Age = 30;
    public string Name
    {
    get { return this._name; }
    set { this._name = value; }
    }
    }
2、在测试页面testPro1.aspx页面,在它的cs代码中加入

</>code

  1. [AjaxPro.AjaxMethod]
    public Student GetStudent()
    {
    //服务端添加GetStudent方法
    return new Student();
    }
    private Student student = null;
    [AjaxPro.AjaxMethod]
    public void SetStudent(Student stu)
    {
    this.student = stu;
    string name = this.student.Name;
    }
3、aspx页面的javascript脚本
测试aspx页面中的脚本

</>code

  1. <head id="Head1" runat="server">
    <title>ajaxPro测试</title>
    <script type="text/javascript">
    function getStudent() {
    var stu = testPro1.GetStudent().value;
    alert(stu.Name
    + " " + stu.Age); //客户js可以访问服务端返回的对象
    }
    function putStudent() {
    var stu = testPro1.GetStudent().value;
    stu.Name
    = "刘宁";
    testPro1.SetStudent(stu);
    //客户提交对象,并且对象的Name字段已经改变为“刘宁”了。
    alert(stu.Name + " " + stu.Age); //客户js可以访问服务端返回的对象
    }
    </script>
    </head>

<div><input id="Button3" type="button" value="获是服务器对象" onclick="getStudent()" />
 <input id="Button4" type="button" value="客户端提交对象给服务器" onclick="putStudent()" />
</div>

参考:官网   AjaxPro使用说明

AjaxPro实现定时刷新效果

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (297人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号