Atlas学习手记(9):异步调用Page Method
使用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){
????return?"Hello?:?"?+?s;
}
再為這個方法加上WebMethod的特性,注意這一切我們都是在普通的ASPX頁面中書寫,而不是在.ASMX中:
[System.Web.Services.WebMethod]public?string?EchoString(string?s)
{
????return?"Hello?:?"?+?s;
}
2.加入ScriptManager控件,這時候因為是Page Method,所以不再需要引入Web Service了
<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?type="text/javascript"?language="JavaScript">????function?OnbuttonGo_click()?
????{
????????requestSimpleService?=?PageMethods.EchoString(
????????????document.getElementById('inputName').value,???????//params
????????????OnComplete????//Complete?event
????????????);
????????return?false;
????}
????function?OnComplete(result)?
????{
????????alert(result);
????}
</script>
編譯運行:
調用后:
二.與WebService Method區別
通過上面的例子大家可能看到了,似乎這種方式與調用WebService Method在PageMethods那兒有一點不同之外,其他的都差不多,為什么還會出現這種方式呢?現在如果我們需要在Web Method里面調用頁面上的控件,那這種方式就很有價值了,對上面的例子作一下小小的改動,首先對TextBox加上runat=server屬性,讓它運行在服務端:
<input?id="inputName"?runat="server"/>再修改Page Method如下:
[System.Web.Services.WebMethod]public?string?EchoString()
{
????return?"Hello?:?"?+?this.inputName.Value;
}
調用的JS代碼:
<script?type="text/javascript"?language="JavaScript">????function?OnbuttonGo_click()?
????{
????????requestSimpleService?=?PageMethods.EchoString(
????????????OnComplete????//Complete?event
????????????);
????????return?false;
????}
????function?OnComplete(result)?
????{
????????alert(result);
????}
</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。
完整示例下載:http://files.cnblogs.com/Terrylee/PageMethodDemo.rar
轉載于:https://www.cnblogs.com/Terrylee/archive/2006/07/31/Atlas_Calling_PageMethod.html
總結
以上是生活随笔為你收集整理的Atlas学习手记(9):异步调用Page Method的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [原创]IrrLicht的GUI使用
- 下一篇: 经济常识