1、添加應用(Ajax.dll)。
2、配置Web.config.
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
</system.web>
3、c#using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(_Default));
}
[Ajax.AjaxMethod()]
public string ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
}
4、aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Ajax實現調用伺服器端方法測試</title>
<script language="javascript" type="text/javascript">
<!--
function ServerMethods()
{
_Default.ServerSideAdd(100,99, ServerSideAdd_CallBack);
}
function ServerSideAdd_CallBack(response){
alert(response.value);
}
function Button1_onclick() {
ServerMethods();
}
// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick()" />
</form>
</body>
</html>