zoukankan      html  css  js  c++  java
  • Ajax的简单小例子

    1.首先下载ajax.dll,一个百度一下都有下载的!自行查找。

    2.把ajax.dll导入到工程。右键工程-->添加引用--->浏览,找到下载好的ajax.dll文件,点击确定,这时候在工程目录下多了一个bin文件夹,里面就有ajax.dll文件,这证明引入ajax.dll成功了。

    3.设置配置文件web.config。

    在Web.config文件下的 <system.web>节点里面添加以下代码即可:

    本地配置  
    <httpHandlers>
        <add  verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
    </httpHandlers>
    
    IIS中配置
    <handlers>
      <!--这一行是为了调用 AJAX.NET 手动加上去的-->
      <add name="ddd" verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />//需要加上name属性,不然会报出注册的类没有定义
    </handlers>

    4.使用演示:

    4.1首先要对ajax进行注册。 在aspx.cs代码中的Page_Load方法里面对ajax进行注册,注册方式为Ajax.Utility.RegisterTypeForAjax(typeof(命名空间.类名)),假如没有命名空间可以直接写类名。代码如下:

    [html] 
     
    public partial class ObjManage : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage));  
        }  
    } 

    4.2编写cs的方法,供javascript调用。cs方法前端必须要有[Ajax.AjaxMethod],然后方法必须是公有public、静态static。例如:

     
       
    [Ajax.AjaxMethod]  
     public static string getString(string str)  
     {  
         string strResult = "The string is " + str;  
         return strResult;  
     }  

    4.3javascript调用cs方法。调用的格式是:类名.方法名(参数),例如:

     
    function alertString() {  
                var str = ObjManage.getString("myAjax").value;  
                alert(str);  
            }  


    这样就完成了。这个是通过测试的,假如有什么问题,可留言。下面给出完成的源码,对于Web.config的代码就不给了,自己安装第3步设置配置文件web.config进行设置就OK了。cs代码:

     
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
      
    public partial class ObjManage : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage));  
        }  
      
        [Ajax.AjaxMethod]  
        public static string getString(string str)  
        {  
            string strResult = "The string is " + str;  
            return strResult;  
        }  
    }  

    aspx代码:

     
    <head runat="server">  
        <title></title>  
        <script type="text/javascript">  
            function alertString() {  
                var str = ObjManage.getString("myAjax").value;  
                alert(str);  
            }  
        </script>  
    </head>  
    <body>  
        <form id="form1" runat="server">  
        <div>  
            <input type="button" value="获取信息" onclick="alertString();" />  
        </div>  
        </form>  
    </body>  

    5.界面展示

     

  • 相关阅读:
    9.11练习:文件访问,写一个逐页显示文本文件的程序,提示输入一个文件名,每次显示文本文件的25行,暂停并向用户提示“按任意键继续”,按键后继续执行。
    关于将缓冲器内容写入文件
    关于移动文件指针
    关于file.writelines换行符的添加
    石头剪子布游戏
    关于浅拷贝,深拷贝
    关于list.extend(iterable)
    vs2012编译openssl
    Sublime Text 3 安装及插件推荐
    bower--包管理工具
  • 原文地址:https://www.cnblogs.com/hongmaju/p/4935326.html
Copyright © 2011-2022 走看看