在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>
欢迎任何形式的转载,但请务必注明出处。