Learn Ajax to do an asynchronous invocation example

Source: Internet
Author: User
Read the example on the website of Okajax, learn Ajax, oneself imitate done, really good, the following his code announced;
First, create a new page, the HTML code to overwrite the original HTML,. cs File No need to thank any code
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default.aspx.cs" Inherits = "_default"%&gt ;

<! 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 >
< BODY >
< form ID = "Form1" runat = "server" >
Zip Code: < Asp:textbox ID = "ZipCode" runat = "server" onKeyUp = "zipchanged ()" ></Asp:textbox >
Cities: < Asp:textbox ID = "City" runat = "server" ></asp:textbox >& nbsp;
</form >
</Body >
</HTML >

Then in the new page, called Default2.aspx, and then, put the inside except the < @Page > This line does not delete, the other code, all erased
Finally, this. cs file, file cover out your CS file can
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 ("Hebei");
break;
Case "010":
Response.Write ("Beijing");
break;

}
}
catch (System.Exception E1)
{

}
}
}

The above is, can test OH.

Related Article

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.