HelloWord,第一个Atlas例子

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=" 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类代码如下:

原文地址:https://www.cnblogs.com/Finding2013/p/467718.html