Author: Dflying Chen ( http://dflying.cnblogs.com/ )
The previous series of articles all directly call a single Web Service. However, in actual project development, especially in the conversion of existing projects, extracting the logic in the page into a specialized Web Service often leads to Comes with quite a bit of work. Atlas takes this into consideration and allows you to add the [WebMethod] attribute to the server-side public method to allow direct calls from client-side JavaScript.
To allow clients to directly call methods defined in ASPX pages, you need to specify the method as public and add the [WebMethod] attribute, such as the following server-side method defined in the ASPX file:
<script runat="server">
[WebMethod]
public int AddInt(int int1, int int2)
{
return int1 + int2;
}
</script>
On the client side, Atlas will Mashup an AddInt JavaScript method for you, which exists in a special namespace PageMethods, so that you can call the above method through PageMethods.AddInt().
At the same time, by defining the WebMethod into the ASPX page, you can also access the values and ViewState of the server-side controls on all pages in this method, and the life cycle of the entire page will be the same as the traditional ASP.NET page PostBack, such as Page_Load All other methods will be called, allowing us to have stronger access to the page. However, this also brings a performance discount, because every time the Web Method is called, the ViewState and control values on the page will be passed back to the server, and the server-side processing of the entire page life cycle will be longer than just processing a pure The Web Method defined in ASMX is much more complicated. So here I recommend using pure Web Service as much as possible. Please refer to: It is recommended to use Web Service instead of Page Method in Atlas server-side implementation.
Let's look at an example. First, let's define the WebMethod in ASPX. You can see that it not only finds the sum of two numbers, but also accesses the value of the server-side TextBox on a page:
<script runat="server">
[WebMethod]
public string AddInt(int int1, int int2)
{
return (int1 + int2).ToString() + string.Format("rnAnd the Server TextBox's Text is '{0}'.", tbServer.Text);
}
</script>
Then there is the ScriptManager of the page. There is no need to add any references here: <atlas:ScriptManager ID="scriptManager" runat="server" />
Then there are two inputs used to enter the addend and an input used to trigger the server call:
<input id="value1" type="text" value="1" />
<input id="value2" type="text" value="2" />
<input id="btnAdd" type="button" value="Add!" onclick="return btnAdd_onclick()" />
There is also a server-side TextBox:
<asp:TextBox ID="tbServer" runat="server" Text="Server control"></asp:TextBox>
The last is the JavaScript call. Pay attention to the built-in namespace of PageMethods:
function btnAdd_onclick() {
PageMethods.AddInt(
$('value1').value,
$('value2').value,
OnComplete
);
}
functionOnComplete(result)
{
alert(result);
}
Run it in the browser, enter two addends, then enter some characters in the TextBox on the server side, click Add, you can see that the value of the TextBox on the server side has been accessed:
This is the network transmission intercepted by Fiddler. You can see that both ViewState and TextBox are passed back to the Server:
The source code of this example program can be downloaded here: