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>