Ajax&Atlas(一)调用WebService

1.         简介

Atlas是微软提供给开发者的Ajax开发包,用于简化ajax,实现富客户端。可以在这个地址下载用于vs2005Atlas项目模板,http://msdn.microsoft.com/asp.net/info/future/atlastemplate/下面是Atlas的整体架构图。(摘自Nikhil KothariPPT)

1.         示例分析

l         调用webservice

通过使用,发现Atlas调用webservice非常简单,先看一段代码,下面将对代码进行分析。

<head id="Head1" runat="server">
  
<atlas:ScriptManager runat="server" ID="scriptManager">
    
<services>
      
<atlas:servicereference path="HelloWorldService.asmx" 
        type
="text/javascript" />
    
</services>
  
</atlas:ScriptManager>
</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> 
<script type="text/javascript">

  
function DoSearch()
  
{
    
var SrchElem = document.getElementById("SearchKey");
    Samples.AspNet.HelloWorldService.HelloWorld(SrchElem.value, 
      OnRequestComplete);
  }


  
function OnRequestComplete(result) 
  
{
    
var RsltElem = document.getElementById("Results");
    RsltElem.innerHTML 
= result;
  }


</script>

</body>

通过上面的一段代码,我们可以发现,调用webserivce代码只有这样一段。

<atlas:ScriptManager runat="server" ID="scriptManager">
    
<services>
      
<atlas:servicereference path="HelloWorldService.asmx" 
        type
="text/javascript" />
    
</services>
  
</atlas:ScriptManager>

对于一般的开发者来说,调用webserivce所要做的只是在<head></head>中添加一个ScriptManager,然后在services中添加servicereference,并设置其属性为所要调用的*.asmx文件。

而对于调用部分,只需在<Script></Script>中使用javascript完成,只需要一句就完成了。

 

 Samples.AspNet.HelloWorldService.HelloWorld(SrchElem.value, 
      OnRequestComplete);


 

Samples.AspNetwebservice的命名空间,HelloWorld是方法,SrchElem.value是从TextBox中取得的值,而OnRequestComplete是另一个函数,用于调用完后的结果处理。

如果不想使用javascript,可以使用一个well-formedXTML,需要两个步骤:

l         <head></hea>中添加一个atlas:ScriptManager

<atlas:ScriptManager runat="server" ID="scriptManager" />

它的作用是生成客户端的xml scriptxml scriptAtlas的客户端脚本语言,采用xml格式,语法比较像asp.net的控件声明语法。每个服务器控件都会生成htmlxml script中的一种或两种。服务器控件向ScriptManager注册自己要生成的xml script, ScriptManager 管理并生成所有的xml script。(一个页面只能有一个ScriptManager)


 

l         <body></body>添加一个XML Script的例子,代码如下。

<script type="text/xml-script">
   
<page 
     xmlns:script
="http://schemas.microsoft.com/xml-script/2005">
    
<components>
      
<textBox id="SearchKey" />
      
      
<serviceMethod id="helloService" url="HelloWorldService.asmx" 
        methodName
="HelloWorld">
        
<bindings>
          
<binding dataContext="SearchKey" dataPath="text" 
            property
="parameters" propertyKey="query" />
        
</bindings>
        
<completed>
          
<invokeMethod target="resultsBinding" 
            method
="evaluateIn" />
        
</completed>
      
</serviceMethod>
      
      
<button targetElement="SearchButton">
        
<click>
          
<invokeMethod target="helloService" method="invoke" />
        
</click>
      
</button>
      
      
<label targetElement="results">
        
<bindings>
          
<binding id="resultsBinding" dataContext="helloService" 
            dataPath
="response.object" property="text" 
            automatic
="false" />
        
</bindings>
      
</label>        
    
</components>
   
</page>
  
</script>

其中components中放一些UI,以及要调用的方法。对于UI,有一个属性targetElement,用于关联页面中所使用控件的ID。如果控件有事件,则在该控件下建立子标签,子标签名为事件名,然后将事件处理放在里面。binding标签中的 dataContext是数据来源的控件的IDdatatext是来源数据的类型。

      

3.         总结

简要介绍了通过atlas调用webservice的两种过程,以及对xml script的一些语法解释,对某些属性还是不太清楚,而且在网上也没找到关于xml script的语法的详细介绍,希望有的朋友发我一份(zmsx44@gmail.com)下一篇将做一个调用Google API的示例。

posted on 2006-02-26 18:44  圣炎¢天乐  阅读(2613)  评论(3编辑  收藏  举报