下载地址:
http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=AjaxPro&DownloadId=2953
首先,添加对AjaxPro.2.dll的引用(对于.NET Framework 1.1 添加AjaxPro.dll)
然后,添加配置文件web.config,添加如下几行:
1
<?xml version="1.0" encoding="utf-8"?>
2
<configuration>
3
<appSettings/>
4
<connectionStrings/>
5
<system.web>
6
<httpHandlers>
7
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
8
</httpHandlers>
9
[
]
10
</system.web>
11
</configuration>
12
2
3
4
5
6
7
8
9
10
11
12
意思是所有的ajaxpro/*.ashx请求都由Ajax.PageHandlerFactory处理,而不是由默认的System.Web.UI.PageHandlerFactory处理程序工厂来处理。
现在我们写个AjaxMethod服务器端方法,他和普通的服务器方法唯一不同的地方就是他必须要在方法的上面添加个[AjaxPro.AjaxMethod],代码如下:
1
[AjaxPro.AjaxMethod]
2
public int AddTwo(int firstInt, int secondInt)
3
2
3
要想在客户端使用Javascrīpt调用.NET方法,你还必须注册这些方法:
1
protected void Page_Load(object sender, EventArgs e)
2
}
2
最后,我们再写客户端脚本来调用服务器方法(代码里有详细的注释)。
以下是前台Default.aspx代码:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml">
5
<head id="Head1" runat="server">
6
<title>Untitled Page</title>
7
</head>
8
<body>
9
<form id="form1" runat="server">
10
<div>
11
<input id="Text1" type="text" onchange="add()"/>
12
+
13
<input id="Text2" type="text" onchange="add()"/>
14
=
15
<span id="result"></span>
16
</div>
17
</form>
18
19
<scrīpt type="text/javascrīpt">
20
function add()
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
以下是完整的后台Default.aspx.cs代码: