环境:VS2005 并在http://ajax.asp.net/default.aspx?tabid=47&subtabid=471下载安装ASP.NET AJAX 1.0 Beta 2
效果:
![](https://i-blog.csdnimg.cn/blog_migrate/11d99c9e144310d2a7461c7a1493d89f.gif)
步骤:
1。在新建网站中新建一个ASP.NET AJAX-Enabled Web Site;
2。添加MyService.asmx,系统自动创建了Default.aspx;
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript" language="JavaScript">
function Sum()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.getElementById('button1').value="等于";
document.getElementById('button2').value="减";
document.getElementById('button3').value="乘";
document.getElementById('button4').value="除";
requestSimpleService = MyService.GetSum(document.getElementById('input1').value,document.getElementById('input2').value, OnRequestComplete);
return false;
}
function Diff()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.getElementById('button1').value="加";
document.getElementById('button2').value="等于";
document.getElementById('button3').value="乘";
document.getElementById('button4').value="除";
requestSimpleService = MyService.GetDiff(document.getElementById('input3').value,document.getElementById('input4').value, OnRequestComplete);
return false;
}
function Mul()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.getElementById('button1').value="加";
document.getElementById('button2').value="减";
document.getElementById('button3').value="等于";
document.getElementById('button4').value="除";
requestSimpleService = MyService.GetMul(document.getElementById('input5').value,document.getElementById('input6').value, OnRequestComplete);
return false;
}
function Quotient()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.getElementById('button1').value="加";
document.getElementById('button2').value="减";
document.getElementById('button3').value="乘";
document.getElementById('button4').value="等于";
requestSimpleService = MyService.GetQuotient(document.getElementById('input7').value,document.getElementById('input8').value, OnRequestComplete);
return false;
}
function OnRequestComplete(result)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
document.getElementById('Flag').innerHTML=result;
//alert(result);
}
</script>
![](/Images/OutliningIndicators/None.gif)
<body>
<form id="form1" runat="server">
<asp:scriptmanager ID="Scriptmanager1" runat="server">
<Services>
<asp:ServiceReference Path="MyService.asmx"/>
</Services>
</asp:scriptmanager>
<table border="0" cellspacing="3" bgcolor="#898989">
<tr bgcolor="#e9e9e9">
<td>
<input type="text" id="input1" size="20" /></td>
<td align="center">
+</td>
<td>
<input type="text" id="input2" size="20" /></td>
<td style="width: 25px">
<input id="button1" type="button" value="和" onclick="return Sum()" /></td>
<td rowspan="4" style="width: 25px"> <label id="Flag" style="color:Red;"></label>
</td>
</tr>
<tr bgcolor="#e9e9e9">
<td>
<input type="text" id="input3" size="20" /></td>
<td align="center">
-</td>
<td>
<input type="text" id="input4" size="20" /></td>
<td style="width: 25px"> <input id="button2" type="button" value="减" onclick="return Diff()" />
</td>
</tr>
<tr bgcolor="#e9e9e9">
<td>
<input type="text" id="input5" size="20" /></td>
<td align="center">
*</td>
<td>
<input type="text" id="input6" size="20" /></td>
<td style="width: 25px"><input id="button3" type="button" value="乘" onclick="return Mul()" />
</td>
</tr>
<tr bgcolor="#e9e9e9">
<td>
<input type="text" id="input7" size="20" /></td>
<td align="center">
/</td>
<td>
<input type="text" id="input8" size="20" /></td>
<td style="width: 25px"><input id="button4" type="button" value="除" onclick="return Quotient()" />
</td>
</tr>
</table>
</form>
</body>
</html>
MyService.cs
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// MyService 的摘要说明
/// </summary>
[Microsoft.Web.Script.Services.ScriptService]
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class MyService : System.Web.Services.WebService
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public MyService ()
{
![](/Images/OutliningIndicators/InBlock.gif)
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
![](/Images/OutliningIndicators/InBlock.gif)
[WebMethod]
public string GetSum(string a, string b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double num1, num2;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1 = Convert.ToDouble(a);
num2 = Convert.ToDouble(b);
return (num1 + num2).ToString();
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "请输入数字";
}
![](/Images/OutliningIndicators/InBlock.gif)
}
[WebMethod]
public string GetQuotient(string a, string b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double num1, num2;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1 = Convert.ToDouble(a);
num2 = Convert.ToDouble(b);
if (num2 == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "被除数不能为零";
}
else
return Math.Round(num1 / num2, 2).ToString();
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "请输入数字";
}
}
[WebMethod]
public string GetDiff(string a, string b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double num1, num2;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1 = Convert.ToDouble(a);
num2 = Convert.ToDouble(b);
return (num1 - num2).ToString();
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "请输入数字";
}
![](/Images/OutliningIndicators/InBlock.gif)
}
[WebMethod]
public string GetMul(string a, string b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
double num1, num2;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1 = Convert.ToDouble(a);
num2 = Convert.ToDouble(b);
return (num1 * num2).ToString();
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "请输入数字";
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)