asp.net|web| page
Author: dflying Chen (http://dflying.cnblogs.com/)
The previous series of articles is a direct call to a single Web service, and in actual project development, especially in the case of existing project transformations, the logic of the page is extracted to a dedicated Web service often brings considerable work. This is fully considered in Atlas, allowing you to add the server-side public method to the [WebMethod] property to allow direct invocation of client JavaScript.
To allow a client to directly invoke a method defined in an ASPX page, you need to specify that the method is public and add the [WebMethod] property, such as the server-side method defined in the ASPX file as follows:
<script runat= "Server" >
[WebMethod]
public int AddInt (int int1, int int2)
{
return int1 + Int2;
}
</script>
At the client, Atlas will create a addint JavaScript method for you, in a special namespace pagemethods, so that you can invoke the above method via Pagemethods.addint ().
Also, you define WebMethod into an ASPX page, where you can also access the values and viewstate of server-side controls on all pages, and the life cycle of the entire page will be the same as the traditional ASP.net page postback, such as Page_ Load and other methods will be called, so that we can have more access to the page. This also brings a performance discount, because every time the Web method is invoked, the values of the ViewState and controls on the page are passed back to the server, and the server-side processing of the entire page's lifetime will be better than just dealing with a purely defined web in ASMX Method is much more complicated. So here I recommend using pure Web service as much as possible, please refer to: The recommended use of Web service instead of page method in the Atlas server-side implementation.
Let's look at an example, first to define the WebMethod in ASPX, to see that there is not just a two-digit number here, but also access to the value of a server-side textbox on a page:
<script runat= "Server" >
[WebMethod]
public string addint (int int1, int int2)
{
Return (Int1 + int2). ToString () + string. Format ("\r\nand the Server TextBox ' s Text is ' {0} '.", Tbserver.text);
}
</script>
Then there's the ScriptManager of the page where you don't need to add any references: <atlas:scriptmanager id= "ScriptManager" runat= "Server"/>
Then two inputs for input addends and input 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!"/>
There is also a server-side textbox:
<asp:textbox id= "Tbserver" runat= "Server" text= "server Control" ></asp:TextBox>
Finally, the JavaScript call, note pagemethods the built-in namespace:
function Btnadd_onclick () {
Pagemethods.addint (
$ (' value1 '). Value,
$ (' value2 '). Value,
OnComplete
);
}
function OnComplete (Result)
{
alert (result);
}
Browser run, enter two addends, and then enter some characters in the server-side textbox, click Add, and you can see the value of the server-side textbox is accessed:
This is the fiddler intercept of the network transmission, you can see ViewState and TextBox are returned to the server: