在VS2008裡已經內建了AJAX的功能,不用像VS2005那樣需要單獨安裝。另外VS2008終于是有JS提示了,哈哈,好玩!

HelloService.asmx
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace Test

{
/**//// <summary>
/// HelloService 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
public class HelloService : System.Web.Services.WebService
public HelloService()
{ }
[WebMethod]
public string HelloWorld(int num1,int num2)
int sum = 0;
try
sum = num1 + num2;
}
catch (Exception ex)
return ex.Message;
return "The sum is:" + sum;
}
}
}

Test.js
function SayClick(param1,param2)

{
var num1= $get(param1).value;
var num2= $get(param2).value;
//命名空間名.類名.函數名(參數清單,回調成功函數名,回調失敗函數名)
Test.HelloService.HelloWorld(num1,num2,CallBack_Succeed,CallBack_Failed);
//回調成功函數
function CallBack_Succeed(resultText)

$get("result").innerHTML=resultText;
//回調失敗函數
function CallBack_Failed(error)

//error.get_message()是擷取異常資訊,由架構提供的
$get("result").innerHTML=error.get_message();

AjaxStudy.aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxStudy.aspx.cs" Inherits="Test.AjaxStudy" %>
<!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">

<Scripts>
<asp:ScriptReference Path="~/JsFile/Test.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/HelloService.asmx" />
</Services>
</asp:ScriptManager>
<br />
<asp:TextBox ID="txtNum1" runat="server"></asp:TextBox>
+<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
<br />
<input id="btnSayHello" type="button" value="AJAX加法" onclick="SayClick('<%=txtNum1.ClientID %>','<%=txtNum2.ClientID %>')" />
<div id="result"></div>
</div>
</form>
</body>
</html>
<a target="_blank" href="http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-12.html">Web 前端工程師和設計師必讀精華文章推薦</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/07/28/html5-awesome-single-page-sites-inspiration.html" target="_blank">酷!15個精美的 HTML5 單頁網站作品欣賞</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/11/22/best-awesome-css3-animation-demos.html">炫!35個讓人驚訝的 CSS3 動畫效果示範</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/03/02/30-mind-blowing-parallax-scrolling-effect-websites.html" target="_blank">贊!30個與衆不同的優秀視差滾動效果網站</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/01/13/25-outstanding-single-page-website-designs.html">靓å!25個優秀的國外單頁網站設計作品欣賞</a>
<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/09/awesome-html5-and-javascript-effects.html">帥!8個驚豔的 HTML5 和 JavaScript 特效</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/06/27/35-exclusive-rainbow-colored-flash-websites.html" target="_blank">頂!35個很漂亮的國外 Flash 網站作品欣賞</a>
<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/24/outstanding-admin-panels-part-one.html" target="_blank">哇!34個漂亮網站和應用程式背景管理界面</a>
<a href="http://www.yyyweb.com/go/web" target="_blank">本部落格新站點 ◆ 前端裡 ◆ 歡迎圍觀:)</a>
歡迎任何形式的轉載,但請務必注明出處。