Author: Dflying Chen ( Http://dflying.cnblogs.com/ )
The previous seriesArticleDirectly call a separateWeb ServiceIn actual project development, especially in the conversion of existing projects, extract the logic on the page to a dedicatedWeb ServiceUsually brings a lot of work.AtlasTaking this into full consideration, you canPublicMethod[Webmethod]Attribute to allow the clientJavascript.
To allow the client to directly call the definition inAspxMethod on the page, you must specify this methodPublicAnd add[Webmethod]Properties, for exampleAspxFile Server method:
< Script runat = " Server " >
[Webmethod]
Public Int Addint ( Int Int1, Int Int2)
{
ReturnInt1+Int2;
}
</ Script >
On the client,AtlasFor youMashupOutputAddintOfJavascriptMethod exists in a special namespacePagemethodsSo that you canPagemethods. addint ()The above method is called.
At the same timeWebmethodDefinedAspxYou can also access the value andViewstateAnd the lifecycle of the entire page will beASP. NETPagePostBackSamePage_loadAnd other methods will be called, so that we can have better access to the page. However, this also brings a performance discount, because each callWeb MethodWhenViewstateAnd the control value is returned to the server, and the server processing the entire page's lifecycle is more than simply processing a pure definition inAsmxInWeb MethodIt is much more complicated. So here I recommend using pureWeb Service, See:InAtlasRecommended for server implementationWeb ServiceInsteadPage Method.
Next let's take a look at an instance, first define inAspxInWebmethodHere, we can see that we do not just calculate the sum of two numbers, but also access the server on a page.TextboxValue:
< 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 the pageScriptmanagerYou do not need to add any references here:
< Atlas: scriptmanager ID = "Scriptmanager" Runat = "Server" />
Then twoInputAndInput:
< 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 serverTextbox:
< ASP: textbox ID = "Tbserver" Runat = "Server" Text = "Server control" > </ ASP: textbox >
FinallyJavascriptCall, notePagemethodsThis built-in namespace:
Function Btnadd_onclick () {
Pagemethods. addint (
$('Value1 '). value,
$ ('Value2'). value,
Oncomplete
);
}
Function Oncomplete (result)
{
Alert (result );
}
Run in the browser, enter two Addons, and thenServerTerminalTextboxAnd clickAdd, You can seeServerTerminalTextboxThe value is accessed:
This isFiddlerThe intercepted network transmission can be seenViewstateAndTextboxAll are returned.Server:
This instanceProgramOfSource codeDownload here: http://files.cnblogs.com/dflying/WebMethodOnPage.zip