Atlas就是Microsoft推出的在asp.net中實現類似Ajax的 rich client 和客户端與服務器異步通信的技術,以下介紹我初步實踐的結果:
首先安裝VS.net2005,2003應該是不可以的。
然後安裝Atlas插件,可在以下地址下載
http://www.microsoft.com/downloads/details.aspx?FamilyId=D746076A-3352-4407-B9D5-832BA4DFFC7B&displaylang=en例子:
在VS2005中新建一個WebSite,添加一個aspx頁面和asmx的webservice類。
在webservice類中添加一個帶參數和返回值得方法,比如 public string HelloWorld(string query) 這樣的,為的是在aspx頁面中調用。
在aspx頁面中添加一段
<atlas:ScriptManager runat=server ID="scriptManager">
<Services>
<atlas:ServiceReference Path="HelloWorldService.asmx" />
</Services>
</atlas:ScriptManager>
可以看出Path那兒就是你寫的那個webservice類。
完整aspx代碼如下:
<%
@ Page Language="C#" Title="Atlas Script Walkthrough" AutoEventWireup="true" CodeFile="AtlasScript.aspx.cs" Inherits="AtlasScript" %>
<!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>Untitled Page</title>
<atlas:ScriptManager runat=server ID="scriptManager">
<Services>
<atlas:ServiceReference Path="HelloWorldService.asmx" />
</Services>
</atlas:ScriptManager>
<style type="text/css">
body {
}{ font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text {
}{ font: 8pt Trebuchet MS }
</style>
</head>
<body>
<form id="Form1" runat="server">
<div>
Search for
<input id="SearchKey" type="text" />
<input id="SearchButton" type="button" value="Search"
onclick="DoSearch()" />
</div>
</form>
<hr style="width: 300px" />
<div>
<span id="Results"></span>
</div> </body>
</html>
<script type="text/javascript">
function DoSearch()
{
var SrchElem=document.getElementById("SearchKey");
HelloWorldService.HelloWorld(SrchElem.value,OnRequestComplete);
}
function OnRequestComplete(result)
{
var RstElem=document.getElementById("Results");
RstElem.innerHTML=result;
}
</script>
可以看出SearchButton按鈕的click事件綁定到了js腳本中的DoSearch()方法上。在DoSearch()方法中獲取客户端輸入文本框SearchKey的值,做為調用webservice方法的參數傳入。js腳本中的OnRequestComplete方法是一個回調方法,用以接收webservice方法的返回值。這樣做是因為你正在進行對Webservice的異步調用,即客户端和服務器是不同步的。因此應該提供一種當服務器方法執
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/**//// <summary>
/// Summary description for HelloWorldService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class HelloWorldService : System.Web.Services.WebService
{
public HelloWorldService ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string query)
{
string inputString = Server.HtmlEncode(query);
if (!String.IsNullOrEmpty(inputString))
{
return String.Format("Hello you queried for {0}.The current time is {1}",
inputString, DateTime.Now);
}
else
{
return "The queried string is null or empty";
}
}
}
行完後通知客户端的機制。該回調函數方法可以是任意一個在js中定義了的function,也可以提供多個回調函數。
webservice類代碼如下: