Learning ASP. NET Ajax (5) services together

Source: Internet
Author: User

In ASP. NET, in addition to the communication methods described in the previous blogs, you can also use the service method for communication, as shown below.

Employee class

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; // <summary> // employee class // </Summary> public class employee {private string _ firstname; private string _ lastname; private string _ title; public Employee () {}/// <summary> /// constructor with parameters /// </Summary> /// <Param name = "firstname"> last name </param> // /<Param name = "lastname"> name </param> // <Param name = "title"> position </param> public employee (string firstname, string lastname, String title) {This. _ firstname = firstname; this. _ lastname = lastname; this. _ Title = title ;}/// <summary> /// surname /// </Summary> Public String firstname {get {return this. _ firstname ;}/// <summary> /// name // </Summary> Public String lastname {get {return this. _ lastname ;}/// <summary> // position /// </Summary> Public String title {get {return this. _ title ;}}}

Client code

<% @ Page Language = "C #" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <SCRIPT runat = "server"> </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml "> 

WebService

<% @ WebService Language = "C #" class = "aspnetajaxoverview. employeeservice "%> using system; using system. web; using system. web. services; using system. web. services. protocols; using system. web. script. services; // This is added by creating a Web Service namespace aspnetajaxoverview {// namespace [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] // to allow ASP. net Ajax calls this web service from the script. Describe downstream comments. // ASP. net Ajax calls this web service from the script (here the default comment needs to be canceled) [system. web. script. services. scriptservice] public class employeeservice: system. web. services. webService {[webmethod] // after adding the following mark, you can use this method [scriptmethod] on the client. // The method here is the code written in C #, which is irrelevant to the front-end Ajax syntax, only called. Public Employee getemployee (string firstname, string lastname, String title) {return new employee (firstname, lastname, title );}}}

Running result

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.