AJAX 指异步JavaScript及 XML(Asynchronous JavaScript And XML),可以实现异步编程、局部刷新,节省客户端和服务器之间来回交互的数据量,本文主要介绍下.net中的Ajax框架AjaxPro的使用。

1、先下载AjaxPro.2.dll,在项目中添加引用。

2、在Web.Config中HttpHandler中添加如下配置:

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

 

3、新建一个Ajax方法集中处理的类文件:

namespace Demo.Common
{
    [AjaxPro.AjaxNamespace("MyAjaxTest")]
    public class Common
    {
        [AjaxPro.AjaxMethod]
        public string GetString(string name)
        {
            return "Hello," + name;
        }
    }
}

 4、新建一个Default.aspx页面,在页面Page_Load中注册Ajax方法:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Page_Load中注册AJAX可调用的类的名称
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Common.Common));
        }
    }

 前端页面使用此Ajax方法的实例:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
       function testAjax(){
          var txtName=document.getElementById("txtName").value;
          MyAjaxTest.GetString(txtName,alertMsg);
       }
       function alertMsg(res){
          alert(res.value);
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txtName" />
    <input type="button" value="test" onclick="testAjax()" /> 
    </div>
    </form>
</body>
</html>

 

posted on 2013-06-02 12:40  零点漂移  阅读(229)  评论(0编辑  收藏  举报