AJAX 入门例子的细节

来源:互联网 发布:陕西出境旅游数据 编辑:程序博客网 时间:2024/04/25 13:28

 主要有两个地方要注意

1. OnClientClick="Change();return false;" 这里注意要加"return false;" ,表示执行完客户端代码后,就不执行服务端单击代码了;否则页面再次刷新,而AJAX传入的值直接被刷掉。

2. Response.End();在服务端返回HTML时,将缓存中的内容返回客户端,停止执行HTML页面,触发HttpApplication.EndRequest事件;否则xmlHttp.responseText中的内容将包括整个页面的内容。

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head id="Head1" runat="server">
  5.     <title>Untitled Page</title>
  6.     <script type="text/javascript">
  7.     var xmlHttp;
  8.     function CreateXMLHttpRequest()
  9.     {
  10.         try 
  11.         {
  12.             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  13.         } 
  14.         catch (e) 
  15.         {
  16.             try 
  17.             {
  18.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  19.             } 
  20.             catch (e2) 
  21.             {
  22.                 xmlHttp = false;
  23.             }
  24.         }
  25.         if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  26.             xmlHttp = new XMLHttpRequest();
  27.         }
  28.     }
  29.     
  30.     function Change()
  31.     {
  32.         CreateXMLHttpRequest();
  33.         var url =  "Default.aspx?UserName=myname";
  34.         xmlHttp.open("GET", url, true);
  35.         //xmlHttp.send(null);
  36.         xmlHttp.send();
  37.         xmlHttp.onreadystatechange = handle;
  38.     }
  39.     
  40.     function handle()
  41.     {
  42.         if (xmlHttp.readyState == 4) 
  43.         {
  44.             //var isValid = xmlHttp.responseText;
  45.             var text = document.getElementById('TextBox1');
  46.             text.value = xmlHttp.responseText;
  47.         }
  48.     }
  49. </script>
  50. </head>
  51. <body>
  52.     <form id="form1" runat="server">
  53.     <div>
  54.         <table>
  55.         <tr>
  56.         <asp:TextBox ID="TextBox1" runat="server">shanghai</asp:TextBox></tr>
  57.         <tr><asp:Label ID="Label1" runat="server" Text="origin"></asp:Label></tr>
  58.         <tr><asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="Change();return false;" /></tr>
  59.             </table>
  60.     </div>
  61.     </form>
  62. </body>
  63. </html>
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. namespace TestWeb
  14. {
  15.     public partial class _Default : System.Web.UI.Page
  16.     {
  17.         protected void Page_Load(object sender, EventArgs e)
  18.         {
  19.             if (null != Request.QueryString["UserName"])
  20.             {
  21.                 string str = Request.QueryString["UserName"].ToString();
  22.                 Response.Write("welcome "+str);
  23.                 Response.End();
  24.             }
  25.         }
  26.     }
  27. }