1.webConfig 中配置一下
System.web 下
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
</httpHandlers>
2.Page_Load 里进行时注册
AjaxPro.Utility.RegisterTypeForAjax(typeof(_AjaxPro));
3.创建服务器端方法。方法需加上[AjaxPro.AjaxMethod]标记
[AjaxPro.AjaxMethod]
public int strName(int num1,int num2)
{
int sum = num1 + num2;
return sum;
}
4.aspx调用
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxPro.aspx.cs" Inherits="_AjaxPro" %>
<!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 type="text/javascript">
function strShow(){
var num1=document.getElementById("num1").value;
var num2=document.getElementById("num2").value;
var arg=_AjaxPro.strName(num1,num2).value;//调用后台方法 继承类名.方法名
alert(arg);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="num1" runat="server" Text="1"></asp:TextBox>
<asp:TextBox ID="num2" runat="server" Text="1"></asp:TextBox>
<input type="button" runat="server" id="btnSub" onclick="strShow();" value="按钮" />
</div>
</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>无标题页</title>
<script type="text/javascript">
function strShow(){
var num1=document.getElementById("num1").value;
var num2=document.getElementById("num2").value;
var arg=_AjaxPro.strName(num1,num2).value;//调用后台方法 继承类名.方法名
alert(arg);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="num1" runat="server" Text="1"></asp:TextBox>
<asp:TextBox ID="num2" runat="server" Text="1"></asp:TextBox>
<input type="button" runat="server" id="btnSub" onclick="strShow();" value="按钮" />
</div>
</form>
</body>
</html>