Use ajax.net to implement client callback)

Source: Internet
Author: User

This chapter describes how to use ajax.net to implement callback. As we all know, callback is a new technology in Asp.net 2.0. It is a transitional technology before ajax.net came out. It can also be used to implement a new Ajax-like no-refreshing technology. However, the callback technology is ignored when the ajax.net framework comes out. However, all technologies have their own values and meanings. In some cases, especially for small data transmission, callback is more efficient than Ajax, besides, the callback technology is used in the ajax.net framework, and the page does not need to be inherited.The icallbackeventhandler interface is much simpler.Now let's talk about how to use the callback Technology in the ajax.net framework.

Step 1: Create a WebService

Using callback Technology in the ajax.net framework requires using WebService as the data transmission carrier. Create an Asp.net Ajax-enabled web site first. Add a new WebService and name it simpleservice. asmx.

Then we write a simple WebService. Remember to add the system. Web. Script. Services. scriptservice attribute. Otherwise, we cannot access the WebService on the client.

SpecificCodeFor example:

This is a very simple WebService, and only the helloworld code is modified. When we run this WebService and enter a name at will, we will get a feedback like "hello, somenames.

Run it. If you can see the following results, our simpleservice. asmx is complete.

Step 2: Call WebService

We will return to default. aspx. Because we have created Asp.net Ajax-enabled web site, the scriptmanager component code has been automatically generated on default. aspx. We want to modify it as follows:

<ASP: scriptmanager ID="Scriptmanager1"Runat="Server">
<Services>
<ASP: servicereference Path="Simpleservice. asmx"/>
</Services>
</ASP: scriptmanager>

Write the correct WebService file name in bold.

After modifying the scriptmanager settings, we can start to write JavaScript to callback this WebService.

We first drag an htmlinputtext control and an htmlinputbutton control in the design interface, and then double-click the button control to write code.

The Code is as follows:

<ScriptLanguage ="Javascript"Type ="Text/JavaScript">
// <! CDATA [ FunctionButton1_onclick (){
Ret = simpleservice. helloworld (document. getelementbyid ('Text1'). Value, oncomplate, ontimeout, onerror );
Return true;
} FunctionOncomplate (ARG)
{
Alert (ARG );
} FunctionOntimeout (ARG)
{
Alert ("Timeout!");
} FunctionOnerror (ARG)
{
Alert ("Error!");
} //]>
</Script>

FunctionOncomplate is the function executed by the client after successful call. ontimeout is the function executed by the client after the server response times out. onerror is the function executed by the client when an error occurs.

Note:: In JavaScriptSimpleserviceIs the class name of simpleservice. asmx, not the file name. If you change the class name of WebService, the name here must be changed.Simpleservice. asmxIt is the file name, which must be clearly divided.

OK, run!

End:

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.