<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!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>无标题页</title>
</head>
<script language="javascript">
function load(provinceid)
{
var drp2 = document.getElementById("DropDownList2");
var len=drp2.options.length;
while(len>0)
{
drp2.options.remove(0);
len=drp2.options.length;
}
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP"); //发送请求
var oDoc = new ActiveXObject("MSXML2.DOMDocument"); //响应结果
if(provinceid==null)
{
var provinceid=document.getElementById("DropDownList1").value;
}
oHttpReq.open("post","Default9.aspx?pid="+provinceid, false);
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items = oDoc.selectNodes("//city/Table");
for (var item = items.nextNode();item;item = items.nextNode())
{
var city = item.selectSingleNode("cityname").nodeTypedValue;
var id = item.selectSingleNode("id").nodeTypedValue;
var newOption = document.createElement("OPTION");
newOption.text = city;
newOption.value = id;
drp2.options.add(newOption);
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 88px; POSITION: absolute; TOP: 32px" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 32px" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
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;
using System.Data.SqlClient;
using System.Xml;
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["gipsy"]);
SqlDataAdapter da = new SqlDataAdapter("select * from province", con);
DataSet ds = new DataSet("province");
da.Fill(ds);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "provinceName";
this.DropDownList1.DataValueField = "id";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
}
}
}
另一个页面的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;
using System.Xml;
using System.Data.SqlClient;
public partial class Default9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["pid"] != null)
{
int intpid = Convert.ToInt32(Request["pid"]);
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["gipsy"]);
SqlDataAdapter da = new SqlDataAdapter("select id,cityname from city where provinceid=" + intpid + "", con);
DataSet ds = new DataSet("city");
da.Fill(ds);
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
}
}
}