您的位置:首页 > 其它

Atlas学习手记(9):异步调用Page Method

2008-03-25 11:11 465 查看
使用Atlas我们可以调用两种服务端的方法WebService Method和Page Method,在前面的使用中,我们一直都是调用WebService Method,如何去调用一个Page Method?本文将简单的介绍一下这一内容。

主要内容

1.如何调用Page Method

2.与WebService Method区别

一.如何调用Page Method

使用Atlas我们可以调用两种服务端的方法WebService Method和Page Method,在前面的使用中,我们一直都是调用WebService Method,如何去调用一个Page Method?本文将简单的介绍一下这一内容。

1.在页面的.cs文件中,我们先定义一个public的方法:

public string EchoString(string s)

[System.Web.Services.WebMethod]

public string EchoString(string s)

<atlas:ScriptManager ID="ScriptManager1" runat="server">

</atlas:ScriptManager>

3.添加HTML控件

<div>

<h3>

Enter your name:<input id="inputName" />

<input id="buttonGo" type="button" value="GO" onclick="return OnbuttonGo_click()" />

</h3>

</div>
4.编写JS代码调用,注意PageMethods这个类,方法EchoString仍然没变,但它却属于PageMethods类,所有的页面暴露的Page Method都应该属于PageMethods。

</script>
编译运行:

<input id="inputName" runat="server"/>
再修改Page Method如下:

[System.Web.Services.WebMethod]

public string EchoString()

</script>

运行后调用:



可以看到,在Page Method中,我们获取到了运行在服务端的TextBox的值。如果是在WebServer Method中,就不能再这样来实现了,这一点我想就是Page Method最大的价值吧。同时对于WebService Method和Page Method的工作原理也有很大的区别,看看Dflying Chen的解释:

对于Atlas调用Web Service来说,当请求被发送时候,仅仅简单传给服务器方法的参数数据。而对于Atlas调用Page Method来说,传输的数据将会很多,将把表单中所有的域,包括ViewState,一起传送到服务器。在服务器端,它的工作方式也和普通的PostBack很相似:在这个Page Method被调用前,所有的服务器控件将得到它自身的状态。这也正是为什么Page Method中可以访问页面中控件状态的原因。

在实际使用中,我们也是尽可能多的使用WebService Method,只在必要的时候才使用Page Method。

完整示例下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: