a.aspx
html部分
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="a.aspx.cs" Inherits="ASPNET.wzh.shopDoNet.admin_a" %>
<HTML>
<HEAD>
<title>a</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="Javascrīpt" name="vs_defaultClientscrīpt">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<scrīpt>
function load(state){
var drp2 = document.getElementById("DropDownList2");
for(var i = 0;i<=drp2.options.length -1;i++){
drp2.remove(i);
}
var ōHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var ōDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "a.aspx?state="+state, 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("//city").nodeTypedValue;
var newOption = document.createElement("OPTION");
newOption.text = city;
newOption.value = city;
drp2.options.add(newOption);
}
}
</scrīpt>
</HEAD>
<body MS_POSITIONING="flowLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" ōnClick="Button1_Click" Text="Button" />
</form>
</body>
</HTML>
cs部分
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;");
SqlCommand cmd = new SqlCommand("select state from authors group by state", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader(Commandbehavīor.CloseConnection);
DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "State";
this.DropDownList1.DataValueField = "State";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange", "load(this.options[this.selectedIndex].innerText)");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(DropDownList1.SelectedValue+" "+DropDownList2.SelectedValue);
}
b.aspx
cs部分
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request["state"] != null)
{
string state = this.Request["state"].ToString();
SqlConnection con = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select city from authors where state = '" + state + "'", 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();
}
}