Call Web Service in ASP. NET Atlas -- expose web method directly on the ASPX page

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.