第一步,添加引用,把Ajax.dll引用到项目中
第二步,在Web.config配置文件中添加以下结点
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx"
type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
第三步:在类文件中写方法了
示例:
[Ajax.AjaxMethod] //记住,一定要加这个
public int Sum(int a, int b) //注意,这里一定要声明为 public
{
return a + b;
}
第四步:在调用页面的Load事件中注册Ajax事件
示例:
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(要引用的类));
}
第五步:用JS调用方法
示例:
function Sum(a,b)
{
要引用的类.Sum(a,b,SumResponse);
}
function SumResponse(response)
{
alert(response.value);
}
第六步:html控件调用JS
示例:
<input id="btnSum" type="button" value="button" onclick="Sum(5,10);"/>
到这里就已经完成了