1.Ajax.html
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Ajax </title>
<Script type = "text/javascript">
Function loadXMLDoc (txt ){
Var xmlhttp;
If (window. XMLHttpRequest) {// code for IE7 +, Firefox, Chrome, Opera, Safari
Xmlhttp = new XMLHttpRequest ();
}
Else {// code for IE6, IE5
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Var citys = xmlhttp. responseText. split (',');
Document. getElementById ("citys"). length = 1;
For (I = 0; I <citys. length-1; I ++ ){
Document. getElementById ("citys"). add (new Option (citys [I], citys [I]);
}
// Document. getElementById ("myDiv"). innerHTML = xmlhttp. responseText;
}
}
Xmlhttp. open ("GET", "ajax/getdata. aspx? Pro = "+ txt, true );
Xmlhttp. send ();
}
</Script>
</Head>
<Body>
<H2> AJAX <Select id = "Select1" onchange = "loadXMLDoc (this. value)">
<Option> select a province </option>
<Option value = "1"> Jiangsu </option>
<Option value = "2"> Shanghai </option>
</Select>
<Select id = "citys">
<Option> select a city </option>
</Select>
<Div id = "myDiv"> </div>
</Body>
</Html>
2. getdata. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class ajax_getdata: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String [] js = new string [] {"Nanjing", "Suzhou", "Changzhou", "Wuxi", "Zhenjiang "};
String pro = Request. QueryString ["pro"];
If (pro = "1 ")
{
String temp = "";
For (int I = 0; I <js. Length; I ++)
{
Temp = temp + js [I];
Temp = temp + ",";
}
Response. Write (temp );
}
Else
Response. Write ("");
}
}