当前位置:编程学习 > C#/ASP.NET >>

asp.net DropDownList二级联动问题

网上搜的二级联动,第一个DropDownList可以,第二个DropDownList不显示,求解,另外getdata文件中有一句writer.IndentChar='',这个空字符串怎样赋值



   <script type="text/javascript">
        function load(state) {
            var drp2 = document.getElementById("Buildings");
          for (i=drp2.length;i>=0;i--)
          {
              drp2.options.remove(i);
          }
          
          var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
          var oDoc = new ActiveXObject("MSXML2.DOMDocument");
          oHttpReq.open("POST", "getData.aspx?state=" + state, false);
          oHttpReq.send("");
          
          result = oHttpReq.responseText;
          oDoc.loadXML(result);
          items1 = oDoc.selectNodes("//AREA/Table/Id");
          items2 = oDoc.selectNodes("//AREA/Table/Buildingsname");
          
          var itemsLength = items1.length;
          alert(state);
          for (i = 0; i < itemsLength; i++) {
              //将小类的类名和编号赋予DropDownList2
              var newOption = document.createElement("OPTION");
              newOption.text = items2[i].text;
              newOption.value = items1[i].text;
              drp2.options.add(newOption);
          }
      }
      window.onload = function() { load('1'); }
    </script>

C#:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //建立数据源加载第一个DropDownList,也可以默认加载第二个
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
            SqlDataAdapter da = new SqlDataAdapter("select id,areaname from area", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.area.DataSource = ds;
            this.area.DataTextField = "areaname";
            this.area.DataValueField = "id";
            this.area.DataBind();
            //这里是绑定客户端事件,当第一个DropDownList的选项改变时激发下面的事件onchange, 这个事件将调一个客户端load()
            this.area.Attributes.Add("onchange", "load(this.options[this.selectedIndex].value)");
            //this.area.Attributes.Add("onchange", "return alert('OK');");
            //this.Response.Write("this.options[this.selectedIndex].value");
            //RegisterStartupScript("", "<script>alert('OK!')</script>");
        }
    }

    #region web form designer generated code
    protected override void OnInit(EventArgs e)
    {
        //CODEGEN:该调用是asp.net web窗体设计器所必须的
        InitializeComponent();
        base.OnInit(e);
    }
    /// <summary>
    /// 设计器支持所需的方法,不要使用代码编辑器修改此方法的内容
    /// </summary>
    /// <returns></returns>
    /// 
    private void InitializeComponent()
    {
        this.Button1.Click += new System.EventHandler(this.Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    //private void Button1_Click(object sender, System.EventArgs e)
    //{
    //    txtAddress.Text = this.Request.Form["Buildings"].ToString();
    //}

getdata:

    protected void Page_Load(object sender, EventArgs e)
    {
        int areaid=int.Parse(Request["state"].ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
        SqlDataAdapter da = new SqlDataAdapter("select id,Buildingsname from Buildings where areaid=" + areaid, con);
        DataSet ds = new DataSet("Buildingsname");
        da.Fill(ds);

        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,Encoding.UTF8);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;
        writer.IndentChar='';
        writer.WriteStartDocument();
        ds.WriteXml(writer);
        writer.Flush();
        Response.End();
    }
#region web form Designer generated code
   protected override void OnInit(EventArgs e)
   {
       //CODEGEN:该调用是asp.net web窗体设计器所必需的

       InitializeComponent();
       base.OnInit(e);
   }
    private void InitializeComponent()
    {
        this.Load+=new System.EventHandler(this.Page_Load);
    }
#endregion
--------------------编程问答-------------------- 二级联动你可以在dropdownlist的selectchanaged中把第一个dropdownlist选择的值穿过去绑定另一个的dropdownlist值! --------------------编程问答-------------------- Refer these:
http://www.cnblogs.com/insus/archive/2011/07/04/2097059.html
http://www.cnblogs.com/insus/archive/2009/03/13/1411016.html
http://www.cnblogs.com/insus/archive/2012/10/16/2725307.html
http://www.cnblogs.com/insus/archive/2013/05/10/3070348.html --------------------编程问答-------------------- 这个是多么老久的问题了啊,网上的答案一大堆 --------------------编程问答-------------------- 这么多的代码。。 --------------------编程问答-------------------- 别用IndentChar属性了,用IndentChars,设为"\t",这个属性用来实现xml文档的缩进。

          function load(state) {
          var drp2 = document.getElementById("Buildings");
          for (i=drp2.length;i>=0;i--) 是不是应该从drp2.length-1开始吧
          {
              drp2.options.remove(i);
          } --------------------编程问答-------------------- 我也才做过二级联,在网上查了很多资料,后面终于做了出来。我相应的代码放在了上面。你可以去看一下,希望对你有帮助http://download.csdn.net/detail/jakinda_fly/6479933 --------------------编程问答--------------------
引用 5 楼 gclol 的回复:
别用IndentChar属性了,用IndentChars,设为"\t",这个属性用来实现xml文档的缩进。

          function load(state) {
          var drp2 = document.getElementById("Buildings");
          for (i=drp2.length;i>=0;i--) 是不是应该从drp2.length-1开始吧
          {
              drp2.options.remove(i);
          }

"\t"解决了空字符串的问题,但还是没有给第二个下拉列表赋值 --------------------编程问答-------------------- javascript load()方法里
items1 = oDoc.selectNodes("//AREA/Table/Id");
items2 = oDoc.selectNodes("//AREA/Table/Buildingsname");

这两行的 AREA 改成 Buildingsname --------------------编程问答-------------------- 这是result获取信息截图
--------------------编程问答-------------------- 关于XmlTextWriter的IndentChar属性我之前说的有错误,不用'\t',因为你已经设置Indentation为4了,所以只要将IndentChar设为' '就可以 --------------------编程问答--------------------
引用 10 楼 gclol 的回复:
关于XmlTextWriter的IndentChar属性我之前说的有错误,不用'\t',因为你已经设置Indentation为4了,所以只要将IndentChar设为' '就可以

设为''的话报此错误
--------------------编程问答--------------------
引用 8 楼 gclol 的回复:
javascript load()方法里
items1 = oDoc.selectNodes("//AREA/Table/Id");
items2 = oDoc.selectNodes("//AREA/Table/Buildingsname");

OK
这两行的 AREA 改成 Buildingsname

除了改AREA外,还有Table/Id中的Id改成id,这里是区分大小写的 --------------------编程问答--------------------
引用 11 楼 zhangfengyi 的回复:
Quote: 引用 10 楼 gclol 的回复:

关于XmlTextWriter的IndentChar属性我之前说的有错误,不用'\t',因为你已经设置Indentation为4了,所以只要将IndentChar设为' '就可以

设为''的话报此错误
不是空字符,那是个空格 --------------------编程问答-------------------- selectchanaged事件就可以啊 --------------------编程问答-------------------- 这个问题解决了,提交表单时报错

--------------------编程问答-------------------- 通过ajax动态生成页面元素就是这样,把EnableEventValidation设为false就不会报这个错,但是好像这种做法不太高明,还有没有更合理的解决办法我也不知道。
而且你要想取第二个下拉框的值也不能直接在后台代码里取,你要给第二个下拉框的name属性设一个值,然后在Page_Load方法中通过 Request["name属性值"] 去取,首先要判断是否为null,不为空的情况下再通过ToString()得出第二个下拉框的所选值
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,