Ajax实现调用服务器端方法
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#<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
</system.web>
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、aspxpublic 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;
}
}
<%@ 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>
<!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>