Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace ComputingWebService
{
/// <summary>
/// Summary description for Computing
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Computing : System.Web.Services.WebService, IComputing
{
#region IComputing Members
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public Point AddPoint(Point a, Point b)
{
return new Point { X = a.X + b.X, Y = a.Y + b.Y };
}
#endregion
}
}
2. 创建Web Application 页面,添加ScriptManager及UpdatePanel,并添加控件。using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace ComputingWebService
{
/// <summary>
/// Summary description for Computing
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Computing : System.Web.Services.WebService, IComputing
{
#region IComputing Members
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public Point AddPoint(Point a, Point b)
{
return new Point { X = a.X + b.X, Y = a.Y + b.Y };
}
#endregion
}
}
3. 在ScriptManager中,添加ServiceReference。
4. 添加OnClientClick事件javascript代码,及callback代码。
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxWebForm.aspx.cs" Inherits="ComputingWebApplication.AjaxWebForm" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:2754/Computing.asmx"/>
</Services>
</asp:ScriptManager>
<script language="javascript">
function Button1_ClientClick()
{
var a = $get("TextBoxA").value;
var b = $get("TextBoxB").value;
ComputingWebService.Computing.Add(a, b, onComputingAddSucceeded);
}
function onComputingAddSucceeded(result)
{
var c = result;
$get("TextBoxC").value = c;
}
function Button2_ClientClick()
{
var ax = $get("TextBoxAX").value;
var bx = $get("TextBoxBX").value;
var ay = $get("TextBoxAY").value;
var by = $get("TextBoxBY").value;
var a = new Object();
a.X = ax;
a.Y = ay;
var b = new Object();
b.X = bx;
b.Y = by;
ComputingWebService.Computing.AddPoint(a, b, onComputingAddPointSucceeded);
}
function onComputingAddPointSucceeded(result)
{
var c = result;
var cx = c.X;
var cy = c.Y;
$get("TextBoxCX").value = cx;
$get("TextBoxCY").value = cy;
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<div>
<asp:TextBox ID="TextBoxA" runat="server" Text="1"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="+"></asp:Label>
<asp:TextBox ID="TextBoxB" runat="server" Text="2"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Button1_ClientClick();return false;" />
<asp:TextBox ID="TextBoxC" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="Label6" runat="server" Text="("></asp:Label>
<asp:TextBox ID="TextBoxAX" runat="server" Text="3" Width="60px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxAY" runat="server" Text="4" Width="60px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text=") + ("></asp:Label>
<asp:TextBox ID="TextBoxBX" runat="server" Text="5" Width="60px"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxBY" runat="server" Text="6" Width="60px"></asp:TextBox>
<asp:Label ID="Label7" runat="server" Text=")"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="Button2_ClientClick();return false;"/>
<asp:Label ID="Label8" runat="server" Text="("></asp:Label>
<asp:TextBox ID="TextBoxCX" runat="server" Width="60px"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxCY" runat="server" Width="60px"></asp:TextBox>
<asp:Label ID="Labe9" runat="server" Text=")"></asp:Label>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxWebForm.aspx.cs" Inherits="ComputingWebApplication.AjaxWebForm" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:2754/Computing.asmx"/>
</Services>
</asp:ScriptManager>
<script language="javascript">
function Button1_ClientClick()
{
var a = $get("TextBoxA").value;
var b = $get("TextBoxB").value;
ComputingWebService.Computing.Add(a, b, onComputingAddSucceeded);
}
function onComputingAddSucceeded(result)
{
var c = result;
$get("TextBoxC").value = c;
}
function Button2_ClientClick()
{
var ax = $get("TextBoxAX").value;
var bx = $get("TextBoxBX").value;
var ay = $get("TextBoxAY").value;
var by = $get("TextBoxBY").value;
var a = new Object();
a.X = ax;
a.Y = ay;
var b = new Object();
b.X = bx;
b.Y = by;
ComputingWebService.Computing.AddPoint(a, b, onComputingAddPointSucceeded);
}
function onComputingAddPointSucceeded(result)
{
var c = result;
var cx = c.X;
var cy = c.Y;
$get("TextBoxCX").value = cx;
$get("TextBoxCY").value = cy;
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<div>
<asp:TextBox ID="TextBoxA" runat="server" Text="1"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="+"></asp:Label>
<asp:TextBox ID="TextBoxB" runat="server" Text="2"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Button1_ClientClick();return false;" />
<asp:TextBox ID="TextBoxC" runat="server"></asp:TextBox>
</div>
<div>
<asp:Label ID="Label6" runat="server" Text="("></asp:Label>
<asp:TextBox ID="TextBoxAX" runat="server" Text="3" Width="60px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxAY" runat="server" Text="4" Width="60px"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text=") + ("></asp:Label>
<asp:TextBox ID="TextBoxBX" runat="server" Text="5" Width="60px"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxBY" runat="server" Text="6" Width="60px"></asp:TextBox>
<asp:Label ID="Label7" runat="server" Text=")"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="Button2_ClientClick();return false;"/>
<asp:Label ID="Label8" runat="server" Text="("></asp:Label>
<asp:TextBox ID="TextBoxCX" runat="server" Width="60px"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text=","></asp:Label>
<asp:TextBox ID="TextBoxCY" runat="server" Width="60px"></asp:TextBox>
<asp:Label ID="Labe9" runat="server" Text=")"></asp:Label>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
在.net 3.5中,一切变得这样轻松自然!