<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax2.aspx.cs" Inherits="Aspx_Ajax2" %>
<!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 runat="server">
<title>Ajax联动菜单</title>
<script language="javascript">
var req;
window.onload = function(){ }
function Change_Select()
{
var zhi=document.getElementById('hero').value;
var url="Ajax2.aspx?id="+escape(zhi);
if( window.XMLHttpRequest )
{
req=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if( req )
{
req.open("GET",url,true);//true异步;false同步
req.onreadystatechange = callback;
req.send( null );
}
}
function callback()
{
if( req.readyState == 4 )
{
if( req.status == 200 )
{
parseMessage();
}
else
{
alert("Not able to retrieve description"+req.statusText);
}
}
}
function parseMessage()
{
alert( req.responseText );
var xmlDoc = req.responseXML;//取xml ,取text
var xSel = xmlDoc.getElementsByTagName("root")[0].childNodes;
//清除下来菜单
var select_root = document.getElementById('skill');
select_root.options.length = 0;
for(var i=0;i<xSel.length;i++)
{
var xValue =xSel[i].childNodes[0].firstChild.nodeValue;
var xText =xSel[i].childNodes[1].firstChild.nodeValue;
var option = new Option( xText,xValue );
try
{
select_root.add( option );
}
catch(e){}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<TABLE width="70%" >
<TR>
<TD align="center">Double Select Box</TD>
</TR>
<TR>
<TD>
<SELECT name="hero" id="hero" onChange="Change_Select()">
<OPTION value="0">Unbounded</OPTION>
<OPTION value="1">D.K.</OPTION>
<OPTION value="2">NEC.</OPTION>
<OPTION value="3">BOSS</OPTION>
</SELECT>
<SELECT name="skill" id="skill">
<OPTION value="0">Unbounded</OPTION>
</SELECT>
</TD>
</TR>
</TABLE>
</div>
</form>
</body>
</html>
----后台---------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if ( Request.QueryString["id"] != null)
{
Response.ContentType = "application/xml";//设置输出流的类型和编码;
string targetId = Request.QueryString["id"].ToString();
string xml_start = "<root>";
string xml_end = "</root>";
string xml = "";
if (targetId == "0")
{
xml = "<select><value>0</value><text>Unbounded</text></select>";
}
else if (targetId =="1")
{
xml = "<select><value>1</value><text>Mana Burn</text></select>";
xml += "<select><value>2</value><text>Death Coil</text></select>";
xml += "<select><value>3</value><text>Unholy Aura</text></select>";
xml += "<select><value>4</value><text>Unholy Fire</text></select>";
}
else if (targetId == "2")
{
xml = "<select><value>1</value><text>Corprxplode</text></select>";
xml += "<select><value>2</value><text>Raise Dead</text></select>";
xml += "<select><value>3</value><text>Brilliance Aura</text></select>";
xml += "<select><value>4</value><text>Aim Aura</text></select>";
}
else
{
xml = "<select><value>1</value><text>Rain of Chaos</text></select>";
xml += "<select><value>2</value><text>Finger of Death</text></select>";
xml += "<select><value>3</value><text>Bash</text></select>";
xml += "<select><value>4</value><text>Summon Doom</text></select>";
}
string last_xml = xml_start + xml + xml_end;
Response.Write( last_xml );
Response.End();
}
}