asp.net中操作xmlhttpRequest對象的ResponseText屬性

來源:互聯網
上載者:User
  1. //webform1.aspx頁面
  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.         
  13.         //建立XMLHttpReqest對象ajax的核心對象
  14.         //導步調用
  15.       var xmlHttp;
  16.       function CreateXMLHttpRequest()
  17.       {
  18.       //如果是ie
  19.          if(window.ActiveXObject)
  20.          {
  21.             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");    
  22.             
  23.          }
  24.          //如果產Firefox等其它瀏覽器在ie7.0以上版本中也有此對象
  25.          else
  26.          {
  27.            xmlHttp=new XMLHttpRequest();
  28.          }
  29.       }      
  30.       function updateTotal()
  31.       {        
  32.         frm =document.forms[0];
  33.       //  url="WebForm2.aspx?A="+frm.elements['A'].value+"&B="+frm.elements['B'].value;
  34.         url="Handler.ashx?A="+frm.elements['A'].value+"&B="+frm.elements['B'].value;
  35.       
  36.       //初始化對伺服器的請求
  37.         xmlHttp.open("GET",url,true);//true表示要非同步呼叫
  38.         xmlHttp.onreadystatechange=doUpdate;//伺服器完成後,用戶端處理事件
  39.         //發送請求
  40.         xmlHttp.send();   
  41.         return false;
  42.       }      
  43.       function doUpdate()
  44.       {
  45.         //表示請示處理完成 
  46.         if(xmlHttp.readyState==4)
  47.         {         
  48.           document.forms[0].elements["TOT"].value=xmlHttp.responseText;  
  49.        //  alert(xmlHttp.responseText);
  50.           
  51.          // document.getElementById('nameList').innerHTML =xmlHttp.responsetext;   
  52.         }
  53.         if(xmlHttp.readyState==0)
  54.         {         
  55.           document.forms[0].elements["TOT"].value="沒有初始化";
  56.         }
  57.         if(xmlHttp.readyState==1)
  58.         {         
  59.           document.forms[0].elements["TOT"].value="裝載中";
  60.         }
  61.         if(xmlHttp.readyState==3)
  62.         {         
  63.           document.forms[0].elements["TOT"].value="互動中";
  64.         }
  65.         
  66.        
  67.         
  68.       }
  69.         </script>
  70.     </HEAD>
  71.     <body onload="CreateXMLHttpRequest();">
  72.         <form>
  73.             <input type="text" id="A" style="Z-INDEX: 101; LEFT: 464px; POSITION: absolute; TOP: 104px"
  74.                 onkeyup="updateTotal();" value="0"> 
  75.                 <input type="text" id="B" style="Z-INDEX: 102; LEFT: 464px; POSITION: absolute; TOP: 160px"
  76.                 onkeyup="updateTotal();" value="0"> 
  77.                 <input type="text" id="TOT" style="Z-INDEX: 103; LEFT: 464px; WIDTH: 216px; POSITION: absolute; TOP: 232px; HEIGHT: 24px"
  78.                 size="30">
  79.                 <div id="nameList" >
  80.                      </div>
  81.             <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 400px; POSITION: absolute; TOP: 112px" runat="server">A</asp:Label>
  82.             <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 400px; POSITION: absolute; TOP: 168px" runat="server">B</asp:Label>
  83.             <asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 400px; POSITION: absolute; TOP: 232px" runat="server">SUM</asp:Label>
  84.         </form>
  85.     </body>
  86. </HTML>
  87. //handler.ashx檔案
  88. <%@ WebHandler Language="C#" Class="Handler" %>
  89. using System;
  90. using System.Web;
  91. public class Handler : IHttpHandler {
  92.     
  93.     public void ProcessRequest (HttpContext context) {
  94.         context.Response.ContentType = "text/plain";
  95.         int numA = int.Parse(context.Request.QueryString["A"]);
  96.         int numB = int.Parse(context.Request.QueryString["B"]);
  97.         int Result = numA + numB;
  98.         context.Response.Write(Result.ToString());
  99.     }
  100.     public bool IsReusable {
  101.         get {
  102.             return false;
  103.         }
  104.     }
  105. }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.