我使用了两个DropDownList进行相关级联,现在有一个问题没有解决,希望高人给一些参考。进行级联时,首次加载没有问题,但当将上面的DropDownList的TextField改变时,问题就出现了。
 if (type_1ddl.SelectedItem.Text != "商品大类")
        {
            Type_1 t = Type_1Manager.getByTypeName(type_1ddl.SelectedItem.Text);
            int typeid1 = t.Typeid_1;
            type_2ddl.DataSource = Type_2Manager.GetType_2ByTypeid_1(typeid1);
            type_2ddl.DataTextField = "typename";
            type_2ddl.DataValueField = "typeid_2";
            type_2ddl.DataBind();
        }
DropDownList2本身使用 <asp:ListItem></asp:ListItem>为其指定了相应的文本,当DropDownList1进行改变时,我想只保留DropDownList2的该文本。

解决方案 »

  1.   

    <asp:ScriptManager ID="ScriptManager1" runat="server">
      </asp:ScriptManager>
      <div>
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
      <asp:DropDownList ID="ddlLB" runat="server" Width="15%" AutoPostBack="True" OnSelectedIndexChanged="ddlLB_SelectedIndexChanged">
      </asp:DropDownList>
      <asp:DropDownList ID="ddlChild" runat="server" Width="20%">
      </asp:DropDownList>
      </ContentTemplate>
      </asp:UpdatePanel>
      </div>  
    protected void ddlLB_SelectedIndexChanged(object sender, EventArgs e)
      {
      if(this.ddlLB.SelectedValue!=null)
      {
      BindChild(this.ddlLB.SelectedValue);
      }
      }
      

  2.   

    您给出的代码我没太明白,我使用DropDownList的DataSource属性为其指定的数据源。
      

  3.   

    在父级下拉框中设置属性 AutoPostBack="True"
    然后在 父级的OnSelectedIndexChanged 事件中 根据 选中的父级ID 初始化 子级下拉框列表(可能需要一个查询函数)
      

  4.   

    DropDownList的级联操作网上很多代码,百度下就能找到。
      

  5.   

    现在级联效果实现了,现在的问题就是当DropDownList1的文本变成自己添加的相应文本后,我想让DropDownList2的<asp:ListItem></asp:ListItem>自己添加的文本仍然保留。
      

  6.   

    重新添加一次呗,DropDownList1每次改变后都先清理DropDownList2,然后绑定DropDownList2数据,然后添加你自己的文本,不行吗?