Operate the responsetext attribute of the XMLHTTPRequest object in Asp.net

Source: Internet
Author: User
  1. // Webform1.aspx page
  2. <% @ Page Language = "C #" inherits = "webapplication2.webform1" codefile = "webform1.aspx. cs" %>
  3. <! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
  4. <HTML>
  5. <Head>
  6. <Title> webform1 </title>
  7. <Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1"/>
  8. <Meta name = "code_language" content = "C #"/>
  9. <Meta name = "vs_defaultclientscript" content = "JavaScript"/>
  10. <Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5"/>
  11. <Script language = "JavaScript" type = "text/JavaScript">
  12. // Create the core object of xmlhttpreqest object Ajax
  13. // Step-by-step call
  14. VaR XMLHTTP;
  15. Function createxmlhttprequest ()
  16. {
  17. // For IE
  18. If (window. activexobject)
  19. {
  20. XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
  21. }
  22. // If Firefox and other browsers have this object in ie7.0 or later versions
  23. Else
  24. {
  25. XMLHTTP = new XMLHttpRequest ();
  26. }
  27. }
  28. Function updatetotal ()
  29. {
  30. FRM = Document. Forms [0];
  31. // URL = "webform2.aspx? A = "+ frm. elements ['a']. Value +" & B = "+ frm. elements ['B']. value;
  32. Url = "handler. ashx? A = "+ frm. elements ['a']. Value +" & B = "+ frm. elements ['B']. value;
  33. // Initialize the request to the server
  34. XMLHTTP. Open ("get", URL, true); // true indicates asynchronous call.
  35. XMLHTTP. onreadystatechange = doupdate; // After the server is complete, the client processes the event
  36. // Send the request
  37. XMLHTTP. Send ();
  38. Return false;
  39. }
  40. Function doupdate ()
  41. {
  42. // Indicates that the request has been processed
  43. If (XMLHTTP. readystate = 4)
  44. {
  45. Document. Forms [0]. elements ["tot"]. value = XMLHTTP. responsetext;
  46. // Alert (XMLHTTP. responsetext );
  47. // Document. getelementbyid ('namelist'). innerhtml = XMLHTTP. responsetext;
  48. }
  49. If (XMLHTTP. readystate = 0)
  50. {
  51. Document. Forms [0]. elements ["tot"]. value = "not initialized ";
  52. }
  53. If (XMLHTTP. readystate = 1)
  54. {
  55. Document. Forms [0]. elements ["tot"]. value = "loading ";
  56. }
  57. If (XMLHTTP. readystate = 3)
  58. {
  59. Document. Forms [0]. elements ["tot"]. value = "interactive ";
  60. }
  61. }
  62. </SCRIPT>
  63. </Head>
  64. <Body onload = "createxmlhttprequest ();">
  65. <Form>
  66. <Input type = "text" id = "A" style = "Z-INDEX: 101; left: 464px; position: absolute; top: pixel"
  67. Onkeyup = "updatetotal ();" value = "0">
  68. <Input type = "text" id = "B" style = "Z-INDEX: 102; left: 464px; position: absolute; top: 160px"
  69. Onkeyup = "updatetotal ();" value = "0">
  70. <Input type = "text" id = "tot" style = "Z-INDEX: 103; left: 464px; width: 216px; position: absolute; top: 232px; Height: 24px"
  71. Size = "30">
  72. <Div id = "namelist">
  73. </Div>
  74. <Asp: Label id = "label1" style = "Z-INDEX: 105; left: 400px; position: absolute; top: Running PX" runat = "server"> A </ASP: label>
  75. <Asp: Label id = "label2" style = "Z-INDEX: 106; left: 400px; position: absolute; top: 168px" runat = "server"> B </ASP: label>
  76. <Asp: Label id = "label3" style = "Z-INDEX: 107; left: 400px; position: absolute; top: 232px" runat = "server"> sum </ASP: label>
  77. </Form>
  78. </Body>
  79. </Html>
  80. // Handler. ashx File
  81. <% @ Webhandler Language = "C #" class = "handler" %>
  82. Using system;
  83. Using system. Web;
  84. Public class handler: ihttphandler {
  85. Public void processrequest (httpcontext context ){
  86. Context. response. contenttype = "text/plain ";
  87. Int NUMA = int. parse (context. Request. querystring ["A"]);
  88. Int numb = int. parse (context. Request. querystring ["B"]);
  89. Int result = NUMA + numb;
  90. Context. response. Write (result. tostring ());
  91. }
  92. Public bool isreusable {
  93. Get {
  94. Return false;
  95. }
  96. }
  97. }

 

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.