AjaxTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax Test.aspx.cs" Inherits="Ajax_Test" %></p><p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head runat="server"><br /> <title>Ajax Test</title><br /> <mce:script type ="text/javascript" ><!--<br />var xmlHttp;</p><p> function createXmlHttpRequest()<br /> {<br /> if(window.XMLHttpRequest)<br /> {<br /> xmlHttp=new XMLHttpRequest();</p><p> if(xmlHttp.overrideMimeType)<br /> {<br /> xmlHttp.overrideMimeType("text/xml");<br /> }<br /> }<br /> else if(window.ActiveXObject)<br /> {<br /> try<br /> {<br /> xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");<br /> }<br /> catch(e)<br /> {<br /> xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br /> }<br /> if(!xmlHttp)<br /> {<br /> window.alert("你的瀏覽器不支援建立XMLhttpRequest對象");<br /> }<br /> return xmlHttp;<br /> }</p><p>//建立CheckUserName</p><p>function CheckNumber(number)<br />{</p><p> createXmlHttpRequest();</p><p> var url="Event.aspx?Number="+number ;</p><p> xmlHttp.open("GET",url,true);</p><p> xmlHttp.onreadystatechange=CheckNumberResult;</p><p> xmlHttp.send(null);</p><p>}</p><p>//建立使用者檢測的回呼函數</p><p>function CheckNumberResult()<br />{<br /> if(xmlHttp.readyState==4)//伺服器響應狀態<br /> {<br /> if(xmlHttp.status==200)//代碼執行狀態<br /> {<br /> if(xmlHttp.responseText=="true")<br /> {<br /> document .getElementById ("txt_Name").value="張三"</p><p> }<br /> else<br /> {<br /> document .getElementById ("txt_Name").value="查無此學號!"<br /> }<br /> }<br /> }<br />}<br />// --></mce:script><br /></head><br /><body><br /> <form id="form1" runat="server"><br /> <div><br /> 請輸入學號:<asp:TextBox ID="txt_Number" runat="server" onkeyup="CheckNumber(document.getElementById('txt_Number').value)"></asp:TextBox>(預設學號:001)<br /> <br /><br /> 姓名: <asp:TextBox ID="txt_Name" runat="server"></asp:TextBox><br /> </div><br /> </form><br /></body><br /></html>
Event.aspx.cs
using System;<br />using System.Collections;<br />using System.Configuration;<br />using System.Data;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.HtmlControls;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Xml.Linq;</p><p>public partial class Event : System.Web.UI.Page<br />{<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /> if (Request.QueryString["Number"].ToString() == "001")<br /> {<br /> Response.Write("true");<br /> Response.End();<br /> }<br /> else<br /> {<br /> Response.Write("false");<br /> Response.End();<br /> }<br /> }<br />}