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

 

主要内容

1.如何调用Page Method

2.与WebService Method区别

 

一.如何调用Page Method

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

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

Atlas学习手记(9):异步调用Page Methodpublic string EchoString(string s)
Atlas学习手记(9):异步调用Page Method
}

再为这个方法加上WebMethod的特性,注意这一切我们都是在普通的ASPX页面中书写,而不是在.ASMX中:

Atlas学习手记(9):异步调用Page Method[System.Web.Services.WebMethod]
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method
public string EchoString(string s)
Atlas学习手记(9):异步调用Page Method
}

2.加入ScriptManager控件,这时候因为是Page Method,所以不再需要引入Web Service

Atlas学习手记(9):异步调用Page Method<atlas:ScriptManager ID="ScriptManager1" runat="server">
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method
</atlas:ScriptManager>
Atlas学习手记(9):异步调用Page Method

3.添加HTML控件

Atlas学习手记(9):异步调用Page Method<div>
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method    
<h3>
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method        Enter your name:
<input id="inputName" />
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method        
<input id="buttonGo" type="button" value="GO" onclick="return OnbuttonGo_click()" />
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method    
</h3>
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method
</div>

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

>

编译运行:

调用后:

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

二.与WebService Method区别

通过上面的例子大家可能看到了,似乎这种方式与调用WebService MethodPageMethods那儿有一点不同之外,其他的都差不多,为什么还会出现这种方式呢?现在如果我们需要在Web Method里面调用页面上的控件,那这种方式就很有价值了,对上面的例子作一下小小的改动,首先对TextBox加上runat=server属性,让它运行在服务端:

Atlas学习手记(9):异步调用Page Method<input id="inputName" runat="server"/>

再修改Page Method如下:

Atlas学习手记(9):异步调用Page Method[System.Web.Services.WebMethod]
Atlas学习手记(9):异步调用Page Method
Atlas学习手记(9):异步调用Page Method
public string EchoString()
Atlas学习手记(9):异步调用Page Method
}

调用的JS代码:

运行后调用:

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

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

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

完整示例下载:https://files.cnblogs.com/Terrylee/PageMethodDemo.rar

相关文章:

  • 2021-07-27
  • 2021-12-25
  • 2021-07-02
  • 2021-12-07
猜你喜欢
  • 2022-02-01
  • 2021-06-16
  • 2021-06-15
  • 2021-11-27
相关资源
相似解决方案