在Page_Load里面:
if(!IsPostBack)
{
if(this.Request["getDataSet"] != null)
{
SqlConnection conn = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("SELECT DISTINCT city,state FROM authors",conn);
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();
}
else
{
SqlConnection conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("SELECT DISTINCT state FROM authors",conn);
DataSet ds=new DataSet();
da.Fill(ds,"state");
this.DropDownList1.DataSource=ds;
this.DropDownList1.DataTextField = "State";
this.DropDownList1.DataValueField = "State";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,new ListItem("--请选择--","-1"));
this.DropDownList1.Attributes.Add("onchange","bindDrp2()");
}
}
客记端js:
var oHttpReq;
var oDoc;
function window.onload()
{
oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "WebForm1.aspx?getDataSet=yes", false);
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
}
function bindDrp2()
{
var drp2 = document.getElementById("DropDownList2");
for (i = drp2.length; i >= 0; i--)
{
drp2.options.remove(i);
}
var newOption = document.createElement("OPTION");
newOption.text = "--请选择--";
newOption.value = "-1"
drp2.add(newOption);
itemCity = oDoc.selectNodes("//city/Table/city");
itemState = oDoc.selectNodes("//city/Table/state");
itemLength = itemCity.length;
for (var i = 0; i < itemLength; i++)
{
if(Form1.DropDownList1.item(Form1.DropDownList1.selectedIndex).value == itemState[i].text)
{
var newOption = document.createElement("OPTION");
newOption.text = itemCity[i].text;
newOption.value = itemCity[i].text;
drp2.add(newOption);
}
}
}