无刷新联动下拉框

在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);
  }
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值