博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Atlas笔记1:介绍和调用WebService的方法

Posted on 2006-09-28 11:44  张明  阅读(259)  评论(0编辑  收藏  举报
Atlas是运行在Asp.net2.0环境下的一个项目,微软的AJAX项目。
Atlas主页http://www.asp.net/default.aspx?tabindex=7&tabid=47

AJAX (Asynchronous JavaScript and XML),结合上.net2.0的功能效果自然非同凡响。网页的也不调用可以避免无谓的网页周期,使浏览速度大大加快,也可以减轻服务器的负荷;用户体验上,可以创建rich UI,制作富有表现力的网页不再那么麻烦。

安装atlas比较简单,只要有asp。net2.0,然后下在一个项目模版文件安装就可以了。

按照Atlas Quickstart Tutorials 的步骤,先开始第一个测试:异步调用WebService。

在atlas项目里面:

1:先创建一个简单的WebService
public class WebServiceT1 : System.Web.Services.WebService
{

    
public WebServiceT1()
    

    }


    [WebMethod]
    
public string TestMethod(string msg)
    
{
        System.Threading.Thread.Sleep(
1000 * 60 * 21);
        
return "the webService got it:"+msg;
    }


}


2:创建一个aspx页面
      首先在页面上添加一个atlas引用:
      注意里面要引用上WebService的地址。幸好2.0里面的代码自动完成功能作的比较多,不噢能够自己写太多代码,输入几个字母就有提示了。
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnableScriptComponents='false'>
                
<Services>
                    
<atlas:ServiceReference Path='WebServiceT1.asmx' />
                
</Services>
            
</atlas:ScriptManager>


然后再页面上加上两个html控件:一个按钮一个文本框。双击文本框,他会自动给创建一个client端的事件,自动创建一个js函数

<input id="Button1" type="button" value="click me and will invoke the webservice" onclick="return Button1_onclick()" />
                
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

js函数
function Button1_onclick()
 
{
   WebServiceT1.TestMethod(document.getElementById('TextBox1').value,OnComplet,OnTimeOut);
//异步调用,传递两个事件的回调函数:完成之后;超时
   
   
return true;
}

function OnComplet(result)
{
//得到返回值.这个result是个objext类型的,可以传递一个类的实例过去,比如DataTable。
  window.alert(result);
}

function OnTimeOut(result)
{
//居然也有返回值,不知道是什么
  window.alert('time out. got nothing from the server');
  window.alert(result);
}

运行测试之就可以了。

几个问题:
1:传递的类型可以是复杂类型,除了string之类也可以一个类的实例。返回值也是。
      可以这样使用:result.Name=xx。详细的介绍会在以后的“atlas数据邦定”中有。
2:好像WebService的方法必须得有一个参数。
3:也可以没有OnTimeOut处理。