AjaxPro入门使用方法

AjaxPro的官网是http://ajaxpro.info,你可以下载到最新的AjaxPro组件。

如果你用的是.NET2.0,你只需要把AjaxPro.2.dll放入应用程序的bin文件夹中,引用下这个DLL就好了,这里需要注意下一般下载解压缩后里面会有 AjaxPro.DLL 和AjaxPro.2.DLL。使用.Net 2.0一直JavaScript报错说未定义‘命名空间和类’,换成AjaxPro.2.Dll就好了。

下面是使用的步骤(别忘了先引用DLL):

1、修改web.config

在system.web节点下添加:

以下为引用的内容:
<system.web>
    <httpHandlers>
      <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
    </httpHandlers>

2、将你的.NET方法添加AjaxMethod属性

以下为引用的内容:
[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}

3、在.NET方法中向客户端注册javascript,用以javascript使用

以下为引用的内容:

namespace MyDemo
{
public class _Default
{
    protected void Page_Load(object sender, EventArgs e)
    {
      AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
    }

    [AjaxPro.AjaxMethod]
    public DateTime GetServerTime()
    {
      return DateTime.Now;
    }
}
}

4、在客户端用javascript调用服务器端的方法,语法也很简单

以下为引用的内容:
function getServerTime()
{
MyDemo._Default.GetServerTime(getServerTime_callback); // asynchronous call
}

// This method will be called after the method has been executed
// and the result has been sent to the client.

function getServerTime_callback(res)
{
alert(res.value);
}

就这样,简单的几步,就已经完成了。在客户端用javascript异步调用服务器端的C#方法,并可以得到服务器端的返回值。

posted @ 2011-07-20 11:06  阿Tai  阅读(536)  评论(1编辑  收藏  举报