Use the Asp.net Ajax framework to achieve interaction between the page without refreshing the server

Source: Internet
Author: User
  1. // Asynccomlayer. ASPX page
  2. <% @ Page Language = "C #" %>
  3. <% @ Register Assembly = "system. Web. Extensions, version = 1.0.61025.0, culture = neutral, publickeytoken = 31bf3856ad364e35"
  4. Namespace = "system. Web. UI" tagprefix = "asp" %>
  5. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <SCRIPT runat = "server">
  7. </SCRIPT>
  8. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  9. <Head runat = "server">
  10. <Title> asynchronous communication layer overview </title>
  11. </Head>
  12. <Body>
  13. <Form ID = "form1" runat = "server">
  14. <Script language = "JavaScript" type = "text/JavaScript">
  15. Function showemployee (firstname, lastname, title)
  16. {
  17. VaR request = new SYS. net. webrequest ();
  18. Request. set_url ('getemployee. ashx ');
  19. Request. set_httpverb ("Post ");
  20. Request. add_completed (ongetemployeecomplete );
  21. VaR requestbody = string. Format (
  22. "Firstname = {0} & lastname = {1} & Title = {2 }",
  23. Encodeuricomponent (firstname ),
  24. Encodeuricomponent (lastname ),
  25. Encodeuricomponent (title ));
  26. Request. set_body (requestbody );
  27. Request. Invoke ();
  28. }
  29. Function ongetemployeecomplete (response)
  30. {
  31. If (response. get_responseavailable ())
  32. {
  33. VaR Employee = response. get_object ();
  34. Alert (string. Format (
  35. "Hello I'm {0} {1}, My title is '{2 }'",
  36. Employee. firstname,
  37. Employee. lastname,
  38. Employee. Title ));
  39. }
  40. }
  41. </SCRIPT>
  42. <Asp: scriptmanager id = "scriptmanager1" runat = "server">
  43. </ASP: scriptmanager>
  44. <Input type = "button" value = "Bill Gates"
  45. Onclick = "showemployee ('bill ', 'gates', 'chair man')"/>
  46. <Input type = "button" value = "Steve Ballmer"
  47. Onclick = "showemployee ('Steve ', 'ballmer', 'ceo ')"/>
  48. </Form>
  49. </Body>
  50. </Html>
  51. // Getemployee. ashx File
  52. <% @ Webhandler Language = "C #" class = "aspnetajaxoverview. getemployee" %>
  53. Using system;
  54. Using system. Web;
  55. Using system. Web. Script. serialization;
  56. Namespace aspnetajaxoverview
  57. {
  58. Public class getemployee: ihttphandler
  59. {
  60. Public void processrequest (httpcontext context)
  61. {
  62. Context. response. contenttype = "text/plain ";
  63. String firstname = context. Request. Params ["firstname"];
  64. String lastname = context. Request. Params ["lastname"];
  65. String title = context. Request. Params ["title"];
  66. Employee Employee = new employee (firstname, lastname, title );
  67. Javascriptserializer serializer = new javascriptserializer ();
  68. String jsonemp = serializer. serialize (employee );
  69. Context. response. Write (jsonemp );
  70. }
  71. Public bool isreusable
  72. {
  73. Get
  74. {
  75. Return false;
  76. }
  77. }
  78. }
  79. }
  80. // Employee. CS File
  81. Using system;
  82. Using system. Data;
  83. Using system. configuration;
  84. Using system. Web;
  85. Using system. Web. Security;
  86. Using system. Web. UI;
  87. Using system. Web. UI. webcontrols;
  88. Using system. Web. UI. webcontrols. webparts;
  89. Using system. Web. UI. htmlcontrols;
  90. Namespace aspnetajaxoverview
  91. {
  92. /// <Summary>
  93. /// Summary description for employee
  94. /// </Summary>
  95. Public class employee
  96. {
  97. Private string _ firstname;
  98. Private string _ lastname;
  99. Private string _ title;
  100. Public Employee (){}
  101. Public Employee (string firstname, string lastname, String title)
  102. {
  103. This. _ firstname = firstname;
  104. This. _ lastname = lastname;
  105. This. _ Title = title;
  106. }
  107. Public String firstname
  108. {
  109. Get
  110. {
  111. Return this. _ firstname;
  112. }
  113. }
  114. Public String lastname
  115. {
  116. Get
  117. {
  118. Return this. _ lastname;
  119. }
  120. }
  121. Public String title
  122. {
  123. Get
  124. {
  125. Return this. _ title;
  126. }
  127. }
  128. }
  129. }

 

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.