转自:http://www.jm-zy.net/Notes/Article-611.html
第一、下载Ajax.dll,到处都可以下。 http://ajax.schwarz-interactive.de/CSharpSample/
第二、新建一个网站项目 AjaxSample
第三、将下载的Aajx.dll解压到项目目下(具体目录不限)
第四、将Ajax.dll引用到项目中。
第五、在Web.Config中,添加Ajax.dll的节:(配置一定要放到<system.web>中)
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
第六、由于这是一个测试项目,所以不考虑太多的架构问题,但为了业务和表现的分开,我们新建一个类文件 test.cs,将业务代码集中在该文件中。类文件存放在 App_Code 目录中。
类文件如下:
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// test 的摘要说明
/// </summary>
public class test
{
public test()
[Ajax.AjaxMethod()]
public string GetText()
{
return "这是一个Ajax测试";
}
}
第七、新建一个页面文件Default.aspx,Default.aspx.cs代码如下所示:
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(test));
}
Default.aspx 如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript">
function ajaxTest()
{ var a = test.GetText().value;
window.alert(a);
}
</script>
</head>
<body onload="ajaxTest();">
<form id="form1" runat="server">
</form>
</body>
</html>
运行后,可以看到最后的效果。
有以下几点说明:
第一、凡是需要在Javascript中调作的函数,均需要在函数上面加上[Ajax.AjaxMethod()](具体的可以参考其它文件)
第二、凡是包含在 Javascript 中调作的函数的类,必须在页面Page_Load中说明:
Ajax.Utility.RegisterTypeForAjax(typeof(test));
其中,test 为业务类名。
https://files.cnblogs.com/wangpei/ajax_dll.rar