看了OkAjax網站上的例子,學習了一下Ajax,自己仿照作了做了,果然挺好,下面他代碼公布一下;
首先,建立一頁面,把一下html代碼覆蓋原html即可,.cs檔案不用謝任何代碼
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default.aspx.cs " Inherits = " _Default " %>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.1//EN " " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > Untitled Page </ title >
< script language = " javascript " type = " text/javascript " >
var zipField = null ;
function zipChanged(){
zipField = document.getElementById( " zipcode " )
var zip = zipField.value;
updateCity(zip);
}
function updateCity(zip) {
var cityField = document.getElementById( " city " );
ask( " Default2.aspx? lookupType=city&zip= " + zip,cityField,zipField);
}
</ script >
< script language = " javascript " type = " text/javascript " >
HTTPRequest = function () {
var xmlhttp = null ;
try {
xmlhttp = new ActiveXObject( " Msxml2.XMLHTTP " );
} catch (_e) {
try {
xmlhttp = new ActiveXObject( " Microsoft.XMLHTTP " );
} catch (_E) { }
}
if ( ! xmlhttp && typeof XMLHttpRequest != ' undefined ' ) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false ;
} }
return xmlhttp;
}
function ask(url, fieldToFill, lookupField)
{
var http = new HTTPRequest();
http.open( " GET " , url, true );
http.onreadystatechange = function ()
{
handleHttpResponse(http, fieldToFill, lookupField)
}
http.send( null );
}
function handleHttpResponse(http, fieldToFill, lookupField)
{
if (http.readyState == 4 )
{
result = http.responseText;
if ( - 1 != result.search( " null " ))
{
lookupField.style.borderColor = " red " ;
fieldToFill.value = "" ;
}
else
{
lookupField.style.borderColor = "" ;
fieldToFill.value = result;
}
}
}
</ script >
</ head >
< body >
< form id = " form1 " runat = " server " >
郵編: < asp:TextBox ID = " zipcode " runat = " server " onKeyUp = " zipChanged() " ></ asp:TextBox >
城市: < asp:TextBox ID = " city " runat = " server " ></ asp:TextBox >& nbsp;
</ form >
</ body >
</ html >
然後在建立一頁面,起名叫Default2.aspx,然後,把裡面除了<@Page>這一行不用刪,其他的代碼,全刪掉
最後把此.cs檔案,檔案覆蓋掉你的cs檔案即可
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
try
{
string sValue = Request.QueryString[ " zip " ].ToString();
switch (sValue)
{
case " 0312 " :
Response.Write( " 河北 " );
break ;
case " 010 " :
Response.Write( " 北京 " );
break ;
}
}
catch (System.Exception e1)
{
}
}
}
以上就是,可以測試一下哦。