Asp.net 2.0 Ajax calls Web Service

Source: Internet
Author: User

 

I. Preparations
First, create an ASP. NET Ajax CTP-enalbed web site
Then, we need three HTML controls and add the following code to aspx:

<Input id = "tbname" type = "text"/>
<Input id = "btnsubmit" type = "button" value = "button"/> <br/>
<Label id = "lblmessage"> </label>

2. Compile a WebService to provide services
Create a web service in the project, we call it helloworld. asmx, and add system. Web. Script. Services reference, scriptservice feature, and a web method gethelloworldcallback to it.
The whole CS code is as follows:

Using system;
Using system. Web;
Using system. collections;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. Web. Script. Services; // Add

/// <Summary>
/// Summary of helloworld
/// </Summary>
[WebService (namespace = "http://www.19870123.cn/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Scriptservice]
Public class helloworld: system. Web. Services. WebService {

Public helloworld (){

// If you use the designed component, uncomment the following line
// Initializecomponent ();
}

[Webmethod]
Public String gethelloworldcallback (string username ){
Return string. Format ("Hello world {0}, the server time is {1}", username, datetime. Now );
}
}

Reference System. Web. Script. Services is required to allow the program to access the service from the client. This webmethod accepts a character parameter and returns it after simple processing.

3. Use the scriptmanager control to reference web services
Return to the ASPX page, add a scriptmanager control, and reference the related WebService file.
Select the scriptmanager control and add a service to the services property in the "properties" window, specifying its path "~ /Helloworld. asmx ". After confirmation, helloworld. asmx will be referenced in scriptmanager. Because some definitions in the program are used in scripts in Microsoft. Web. Preview. previewscript. JS, we also need to add references to it.
Similarly, set the attributes for the scriptmanager control. This time it is the scripts attribute. We add a script and specify its assembly as Microsoft. Web. Preview, and its name as previewscript. JS, so that the page will include the Js. The scriptmanager control code is as follows:

<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "~ /Helloworld. asmx "/>
</Services>
<Scripts>
<Asp: scriptreference Assembly = "Microsoft. Web. Preview" name = "previewscript. js"/>
</Scripts>
</ASP: scriptmanager>

3. Run the program!
After the above work is done, we will enter an exciting moment: Let the program run!
To let the program run, we have to use JavaScript! We first define three variables:

VaR tbname, btnsubmit, lblmessage;

Then, we need related methods to run the program. We inherit the C # syntax and call it the pageload method. The Code is as follows:

Function pageload ()
{
Tbname = new SYS. Preview. UI. Textbox ($ get ('tbname'); // create a textbox
Tbname. initialize (); // complete Initialization

Btnsubmit = new SYS. Preview. UI. Button ($ get ('btnsubmit '));
Btnsubmit. add_click (btnsubmit_click); // Add an event
Btnsubmit. initialize ();

Lblmessage = new SYS. Preview. UI. Label ($ get ('lblmessage '));
Lblmessage. initialize ();
}

Function btnsubmit_click () // Click Event of btnsubmit
{
Helloworld. gethelloworldcallback (tbname. get_text (), callback_helloworld); // call the helloworld method of WebService
}

Function callback_helloworld (result)
{
Lblmessage. set_text (result); // display the callback result
}

 

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.