Ajax asynchronously calls ashx to return the serialization class

Source: Internet
Author: User

Getemployee. ashxCode

<% @ Webhandler Language = "  C #  " Class = "  Aspnetajaxoverview. getemployee  " %> Using  System;  Using  System. Web;  Using  System. Web. Script. serialization;  Namespace Aspnetajaxoverview {  Public   Class  Getemployee: ihttphandler {  Public   Void  Processrequest (httpcontext context) {context. response. contenttype = "  Text/plain  "  ;  String Firstname = context. Request. Params [ " Firstname  "  ];  String Lastname = context. Request. Params [ "  Lastname  "  ];  String Title = context. Request. Params [ "  Title  "  ]; Employee = New Employee (firstname, lastname, title); javascriptserializer serializer = New  Javascriptserializer ();  String Jsonemp = Serializer. serialize (employee); context. response. Write (jsonemp );}  Public   Bool  Isreusable {  Get  {  Return   False ;}}}} 

Employee. CS class code

View code

 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;  Namespace  Aspnetajaxoverview {  ///   <Summary>      ///  Summary Description for employee  ///   </Summary>      Public   Class Employee {  Private   String  _ Firstname;  Private   String  _ Lastname;  Private   String  _ Title;  Public  Employee (){}  Public Employee ( String Firstname, String Lastname, String  Title ){  This . _ Firstname = Firstname;  This . _ Lastname = Lastname;  This . _ Title = Title ;}  Public   String  Firstname {  Get  { Return   This  . _ Firstname ;}}  Public   String  Lastname {  Get  {  Return   This  . _ Lastname ;}}  Public   String  Title {  Get {  Return   This  . _ Title ;}}}} 

Execute the ashx code for a different table

<% @ 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  " > <Head runat = "  Server  " > <Title> asynchronous communication layer overview </title>  "  Form1  " Runat = " Server  " > <Asp: scriptmanager id = "  Scriptmanager1  " Runat = "  Server  " > </ASP: scriptmanager> <script language = "  Javascript  " Type = "  Text/JavaScript  " >Function showemployee (firstname, lastname, title ){  VaR Request = New  SYS. net. webrequest (); Request. set_url (  '  Getemployee. ashx  '  ); Request. set_httpverb (  "  Post  "  ); Request. add_completed (ongetemployeecomplete );  VaR Requestbody = String. Format (  "  Firstname = {0} & lastname = {1} & Title = {2}  "  , Encodeuricomponent (firstname), encodeuricomponent (lastname), encodeuricomponent (title); Request. set_body (requestbody); Request. Invoke ();} function ongetemployeecomplete (response ){  If  (Response. get_responseavailable ()){  VaR Employee =Response. get_object (); alert (string. Format (  "  Hello I'm {0} {1}, My title is '{2 }'  "  , Employee. firstname, employee. lastname, employee. Title ));}} </SCRIPT> <input type = "  Button  " Value = "  Bill Gates  " Onclick = "  Showemployee ('bill ', 'gates', 'chair man ')  " /> <Input type = "  Button  " Value = "  Steve Ballmer  "  Onclick = "  Showemployee ('Steve ', 'ballmer', 'ceo ')  " /> </Form> </body> 

 

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.