VS2008 Ajax功能体验
在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;
}
}
}
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();
}
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 />
<br />
<input id="btnSayHello" type="button" value="AJAX加法" onclick="SayClick('<%=txtNum1.ClientID %>','<%=txtNum2.ClientID %>')" />
<div id="result"></div>
</div>
</form>
</body>
</html>
<%@ 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 />
<br />
<input id="btnSayHello" type="button" value="AJAX加法" onclick="SayClick('<%=txtNum1.ClientID %>','<%=txtNum2.ClientID %>')" />
<div id="result"></div>
</div>
</form>
</body>
</html>