前台Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="_test" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="JS/jquery-1.4.2.min.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
function loadXML(xmlpath) {
var xmlDoc = null;
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
} else if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
} else {
alert('Your browser cannot handle this script.');
}
xmlDoc.async = false;
xmlDoc.load(xmlpath);
return xmlDoc;
}
$(function() {
var xmlDoc = null;
xmlDoc = loadXML("area.xml");
var $s1 = $("#Select1");
var $s2 = $("#Select2");
var $s3 = $("#Select3");
// var v1=null;
// var v2=null;
// var v3=null;
var v1 = "陕西省";
var v2 = "宝鸡市";
var v3 = "陈仓区";
var root = $(xmlDoc).find("address")[0];
$(root).children("province").each(function() {
appendOptionTo($s1, $(this).attr("name"), $(this).attr("name"), v1);
});
$s1.change(function() {
$s2.html("");
var province_node = $(root).children("province")[this.selectedIndex];
$(province_node).children("city").each(function() {
appendOptionTo($s2, $(this).attr("name"), $(this).attr("name"), v2);
});
$s2.change();
}).change();
$s2.change(function() {
$s3.html("");
var province_node = $(root).children("province")[$s1[0].selectedIndex];
var city_node = $(province_node).children("city")[this.selectedIndex];
$(city_node).children("country").each(function() {
appendOptionTo($s3, $(this).attr("name"), $(this).attr("name"), v3)
});
}).change();
function appendOptionTo($o, k, v, d) {
var $opt = $("<option>").text(k).val(v);
if (v == d) { $opt.attr("selected", "selected") }
$opt.appendTo($o);
}
});
function selectChange(from, to) {
alert("From:" + from.selectedIndex + " To: " + to);
alert("From:" + from.value + " To: " + to);
}
</script>
<style type="text/css" media="screen">
select
{
width: 80px;
}
</style>
</head>
<body>
<form runat="server">
<asp:DropDownList runat="server" ID="MyDropDownListSex">
<asp:ListItem>boy</asp:ListItem>
<asp:ListItem>girl</asp:ListItem>
</asp:DropDownList>
<select id="Select1" name="Select1">
</select>
<select id="Select2" name="Select2">
</select>
<select id="Select3" name="Select3">
</select>
</form>
</body>
</html>
后台Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Configuration;
public partial class _test : System.Web.UI.Page
{
XmlDocument xmlDoc = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
MyDropDownListSex.Attributes.Add("onchange", "selectChange(this,'" + this.MyDropDownListSex.ClientID + "')");
if (!IsPostBack)
{
}
}
void Datea()
{
StringBuilder sb = new StringBuilder();
xmlDoc.Load(Server.MapPath("address.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("area/province[@provinceID]");
sb.Append("{Sheng:[");
foreach (XmlNode myNode in nodelist)
{
string name = myNode.Attributes["province"].Value;
sb.Append("{");
sb.AppendFormat("Name:\"{0}\",", name);
sb.AppendFormat("Value:{0}", myNode.Attributes["provinceID"].Value);
sb.Append("},");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("]}");
Response.Write(sb.ToString());
Response.End();
}
void Sheng(string name, string pic, string str)
{
StringBuilder sb = new StringBuilder();
xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
sb.Append("{" + name + ":[");
if (str == "City")
{
XmlNodeList nodelist = xmlDoc.SelectNodes("area/province[@provinceID=" + pic + "]/" + str + "");
foreach (XmlNode obj in nodelist)
{
string dd = obj.Attributes["City"].Value;
sb.Append("{");
sb.AppendFormat("Name:\"{0}\",", dd);
sb.AppendFormat("Value:{0}", obj.Attributes["CityID"].Value);
sb.Append("},");
}
}
else if (str == "Piecearea")
{
XmlNodeList nodelist = xmlDoc.SelectNodes("area/province/City[@CityID=" + pic + "]/" + str + "");
foreach (XmlNode obj in nodelist)
{
string dd = obj.Attributes["Piecearea"].Value;
sb.Append("{");
sb.AppendFormat("Name:\"{0}\",", dd);
sb.AppendFormat("Value:{0}", obj.Attributes["PieceareaID"].Value);
sb.Append("},");
}
}
sb.Remove(sb.Length - 1, 1);
sb.Append("]}");
Response.Write(sb.ToString());
Response.End();
}
}

浙公网安备 33010602011771号