ajax实现DropDownList 联动

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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值