ASP. NET ajax1.0 trial: Web service call

Source: Internet
Author: User

System Environment: Windows Server 2003 + IIS6
Development Environment: Visual Web Developer 2005 quick release + ASP. NET Ajax 1.0
Purpose: to call the Web service on the server without refreshing
Implementation key: scriptmanager + scriptservice

Install ASP. net ajax1.0 and later, an option is added when you create a website in vs: Asp. net Ajax-enabled web site, select this one to create a website that has been configured to use Ajax (mainly Web. config) to make the simplest:

1. Create an ASP. NET Ajax-enabled web site (http/FTP/file system). The default. aspx includes references to scriptmanager.

2. Add a web service file named testservice. asmx to the project,CodeAs follows:

Using System;
Using System. Web;
Using System. collections;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Web. Script. Services; // Note:

[WebService (namespace =   " Http://www.cnblogs.com/dingxue " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
[Scriptservice] // Note:
Public   Class Testservice: system. Web. Services. WebService {

PublicTestservice (){
}

[Webmethod]
Public StringMerge (StringA,StringB)
{
ReturnA+B;
}
}

The two lines with annotations above are required to enable Ajax calls to this web service. This Web Service is very simple. There is a merge method that accepts two string-type parameters and concatenates these two strings and returns them.

3. modify the definition of scriptmanager in the default. aspx file to add a reference to the Web service just now. The Code is as follows:

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

ASP: servicereference allows you to reference a web service. The path attribute indicates the location of the web service file.

4. Add the HTML Control for testing, three text boxes, two for input, one for display results, and one for execution, as shown below:

< Input ID = "Txt1" Size = "5" Type = "Text"   /> +
< Input ID = "Txt2" Size = "5" Type = "Text"   /> =
< Input ID = "Txt3" Size = "10" Type = "Text"   />
< Input Type = "Button" Value = "Merge" Onclick = "Mergeit ()"   />

5. The final implementation of mergetit () is used to implement the final call:

< Script Type = " Text/JavaScript " >
Function Mergeit (){
New Testservice (). Merge ($ get ( " Txt1 " ). Value, $ get ( " Txt2 " ). Value, mergecomplete );
}
Function Mergecomplete (result ){
$ Get ( " Txt3 " ). Value = Result;
}
</ Script >

The preceding button calls mergeit (), which calls testservice and uses new testservice () to call the merge method in Web Service. The parameters must be described here, in the merge method of Web Service, there are only two parameters, but here there are three. The first two parameters are the same as those of Web Service, and the last one is the name of another JS function: mergecomplete, this is used to accept the results of the called method. During execution, the results returned by Web Service are automatically passed into mergecomplete. mergecomplete only accepts one parameter, which is named result.
PS: $ get is the abbreviation of document. getelementbyid. It can be written like this in ASP. NET Ajax, And it looks better to be written.

6. Now, press F5. If nothing happens, you can see the result. Enter characters in the first two text boxes and click the button to view the result.

Project to people is too busy, for a long time don't come, can't help everyone, attached to this example source code download: http://files.cnblogs.com/dingxue/AJAXEnabledWebSite1.rar (2007.8.5)

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.