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