如果您没有做第一个实验请您先做下,这个实验讲的是如何使用atlas宣告块来调用web服务而不要写js脚本,大家应该记得我们在第一个实验里是使用js 脚本来调用web 服务里的方法的,现在不要写了那个js脚本了,直接使用atlas宣告块来调用web services,不知道为什么,我私下认为这个方法还没有上面的那个方法(使用js脚本的方法)来的更简单。
练习一:创建一个asp.net content 页
     在实验的这个部分,你将做一个和实验1一样具有相同的结果和控件的asp.net content页。
1,创建一个content页,在解决方案管理器上个实验里我们创建的Default.master上右击,选Add Content Page.且命名为HelloWorldDeclarative.aspx。
2,切换到
代码视图,删除那两个ContentPlaceHolderId属性值不是Main的元素。在<%@ Page指令行,设置他的Title属性为“Atlas实验2”
3,添加控件到content页面上。在元素里面,添加下面的一些控件标记。
 

<form action="">

  <div>

    Search for

    <input id="SearchKey" type="text" />

    <input id="SearchButton" type="button" value="Search" />

  </div>

</form>

<hr style="width: 300px" />

<div>

  <span id="Results"></span>

</div>
 在这个时候如果我们切换到设计视图,我们就会看到他的设计视图和我们在实验1里所做的那个页面一样。

练习二:创建个atlas宣告块调用web服务,在实验的这个部分,创建atlas宣告块标记,你将能够调用和实验一一样的web服务而不需要写js代码。
1,在代码视图里,在<asp:content>元素里面,紧跟你刚刚创建底</div>标记,写如下的代码:
  <script type="text/xml-script">

  <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">

    <references>

 

    </references>

    <components>

 

    </components>

  </page>

</script>
atlas宣告块使用了标准的xhtml  <script>标记,当然他的type属性值是text/xml-script,这个属性值也是唯一确定他是不是一个atlas宣告块的标识。atlas宣告块的根元素是<page>,他包含了atlas宣告块里面的其他所有元素,同时他也有自己的名字空间,关于这点,大家可以看看xml和xhtml的相关资料。接下来是<references>元素和<components>元素,他们涉及到atlas宣告块的两个主要部分:哪个js脚本文件将被使用(从atlas的js脚本库),哪些界面组件被联系起来。
2,在这个例子里面,在<references>元素里面需要添加下面的两个js文件:
  <add src="ScriptLibrary/AtlasUI.js" />

<add src="ScriptLibrary/AtlasControls.js" />
在<components>元素里面,添加个<textbox>元素,一个<serviceMethod>元素,一个<button>元素,一个<label>元素,代码如下:
 

<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>

 atlas的UI元素被定义成<components>元素的子元素
使用id属性把<textbox>和页面上的我们在最先前写的 textbox控件联系起来了。
<button>标记确定页面上的哪个button按钮去调用远程的方法,targetElement="SearchButton"确定了页面上的id值是SearchButton的按钮去调用远程方法。他的<click>元素确定了页面上按钮的行为是单击事件。在他的<invokeMethod>子元素里面,他的target属性指定了我们刚刚在上面 定义的<serviceMethod>的id。
<serviceMethod>标记提供了将被调用的web 服务的url,他的文件名是:HelloWorldService.asmx

也就是我们在实验1里面创建的那个web 服务。<serviceMethod>元素也提供了将要调用的web服务的方法的名字以及关于这个方法的详细资料。在他的<bindings>标记里面,他指定了哪个控件的值将被当做参数提交给远程方法。在<completed>标记里面,指定了当远程方法完成之后将要做的事。他通过<method>属性指定了一个本地的atlas方法evaluateIn来得到从服务器端返回的值并且把得到的值赋给本地对象<label>,在这里是通过target来指定的,同时<label>的targetElement属性指定了和页面控件id值是results的span关联起来。在<label>的子元素<bindings>指定了如何绑定从远程方法的返回值给页面上的<span>元素。
好了,完成后的代码如下:

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Atlas实验2" %>
<asp:Content ID="Content3" ContentPlaceHolderID="Main" Runat="Server">
   <form action="">
  <div>
    Search for
    <input id="SearchKey" type="text" />
    <input id="SearchButton" type="button" value="Search" />
  </div>
</form>
<hr style="width: 300px" />
<div>
  <span id="Results"></span>
</div>
<script type="text/xml-script">
  <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
    <references>
       <add src="ScriptLibrary/AtlasUI.js" />
       <add src="ScriptLibrary/AtlasControls.js" />
    </references>
    <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>

</asp:Content>

 我们现在把他设置为起师页,按F5运行下,天啊,鼠标刚刚离开那个按钮,结果就在下面显示出来拉!!!

虽然这问状搞完拉,但是心中还是郁闷的,总发觉他没有实验1的方便易懂啊~~~~~~