asp.net 中如何对<dropdownlist>实现多级联动,并且点下面的按钮点击的时候,我还想把它的值给取出来描述:首先,页面加载的时候出现一个<dropdownlist>,并从数据库中查出数据,把值绑定到<dropdownlist>   当我选中<dropdownlist>的值时,把它的子节点的值查出来,如果有值的话,就为他生成一个<dropdownlist>   并把查出来的值给他绑定上。   如果选中第二个<dropdownlist>的时候,把它的子节点的值查出来,如果有值的话,就为他生成一个<dropdownlist>  并把查出来的值给他绑定上依次类推,总之他有多少子节点,我们就为他动态生成<dropdownlist>,并且绑定。最下面的按钮点击的时候,我还想把它的值给取出来,(只要有<dropdownlist>。我都想取他的值)请大家指教,多多帮忙,谢谢。

解决方案 »

  1.   

    既然是无限级,干嘛用非用N个dropdownlist来做
      

  2.   

    吧dropdowlist的AutoPostBack="True"
    然后在onselectedindexchanged事件里面写你要实现的东西就行了
      

  3.   

    http://www.51aspx.com/CV/myAjaxDropdownlist/
      

  4.   

    http://www.51aspx.com/S/%E8%81%94%E5%8A%A8.html
      

  5.   

    参考:http://www.cnblogs.com/insus/articles/1411016.html
      

  6.   

    protected void course_SelectedIndexChanged(object sender, System.EventArgs e) 

        //根據課程顯示該課程的廠 班別 
        Listfac(); 
        ListClass(); 
        setfocu("course"); 
      

  7.   


    用ASP.NET AJAX实现无刷新多个DropDownList的动态联动
    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 字串4<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>DDLajaxtitle>
    head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    asp:ScriptManager>
    <br />
    &nbsp;div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    Width="233px">
    asp:DropDownList><br />
    <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
    Width="233px">
    asp:DropDownList><br />
    <br />字串8
    Default2.cs 字串4using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient; 字串5public partial class Default2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    DropDownList1.AutoPostBack = true;
    DropDownList2.AutoPostBack = true; 字串7if (!IsPostBack)
    {
    DropDownList1.Items.Add("Please Select Country");
    DropDownList2.Items.Add("Please Select State");字串5DataRead("Select Country_ID, Country_Name from Country", DropDownList1); 字串8DropDownList2.Enabled = false;
    Label1.Text = "";
    }
    else if (DropDownList1.SelectedIndex == 0)
    DropDownList2.Enabled = false;
    } 字串7protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    DropDownList2.Items.Clear();
    DropDownList2.Items.Add("Please Select State");字串7
    if (DropDownList1.SelectedIndex == 0)
    DropDownList2.Enabled = false;
    else
    {
    DropDownList2.Enabled = true;
    DataRead("Select State_ID, State_Name from State where State_Country = " DropDownList1.SelectedValue, DropDownList2);
    }
    Label1.Text = "";
    } 字串1protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
    Label1.Text = "You select " this.DropDownList2.SelectedItem ", " this.DropDownList1.SelectedItem;
    if (DropDownList2.SelectedIndex == 0)
    Label1.Text = "";
    } 字串8protected void DataRead(string Sqlstr, DropDownList ddl)
    {
    SqlConnection conn = new SqlConnection("server=localhost;database=DropDownList;uid=sa;pwd=sa");
    conn.Open();
    SqlCommand com = new SqlCommand(Sqlstr, conn);
    SqlDataReader sr = null;
    sr = com.ExecuteReader();
    while (sr.Read())
    ddl.Items.Add(new ListItem(sr[1].ToString(),sr[0].ToString()));
    conn.Close();
    }
    }字串6数据库字串7
    State_ID State_Name State_Country 
    ----------- -------------------------------------------------- ------------- 
    1 NewYork 1
    2 Carifornia 1
    3 Shanghai 2
    4 Beijing 2
    5 Tokyo 3
    6 Osaka 3(6 row(s) affected)字串4Country_ID Country_Name 
    ----------- -------------------------------------------------- 
    1 US
    2 China
    3 Japan 字串6(3 row(s) affected)字串5
    <asp:Label ID="Label1" runat="server" Text="Label" Width="234px">asp:Label>
    ContentTemplate>
    asp:UpdatePanel>
    <br />
    <br />
    <br />
    <br />
    <marquee>This is a trace message that marces across the bottom of the screen.marquee>
    form>
    body>
    html>
      

  8.   

    曾经,我们为了实现dropdownlist的无刷新联动绞尽脑汁,最后还不得不写大量的js代码来实现。,如今,
    微软的atlas,可以让我们摆脱写大段JS代码来实现dropdownlist的无刷新联动,而且简单易懂。一、拖入一个dropdownlist1控件,二、在drp.aspx页面上放一个atlas的核心控件updatepanel1,给updatepanel1增加一个scriptmanager控件,
             注意scriptmanager的属性 EnablePartialRendering必须设为"True",一个页面只能有一个scriptmanager控件三、将一个dropdownlist2控件拖入到updatepanel1区域内,并将其ispostback属性设为true  ,
             现在设置updatepanel1的属性triggers:updatepanel1 的control指定为dropdownlist1,proprety指定为selectvalue;四、再放入一个updatepanel2,在里面再放一个dropdownlist3,         
            现在设置updatepanel2的属性triggers:updatepanel2 的control指定为updatepanel1,proprety指定为triggers;         因为dropdownlist3的数据源是dropdownlist2的索引改变而来,而dropdownlist2在updatepanel1里面,所以这里
             是设置updatepanel2 的control为updatepanel1,proprety指定为triggers;此时,所有的属性基本设置完毕了,开始写服务器端的代码DropDownList1的索引改变事件
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "name";
            DropDownList2.DataValueField = "name";
            DropDownList2.DataBind();
          
        }DropDownList2的索引改变事件
     protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);        DropDownList3.DataSource = ds.Tables[0];        DropDownList3.DataTextField = "address";
            DropDownList3.DataValueField = "address";
            DropDownList3.DataBind();
            }创建数据表SQL
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[test]
    GOCREATE TABLE [dbo].[test] (
     [id] [int] IDENTITY (1, 1) NOT NULL ,
     [name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
     [address] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
     [bianhao] [char] (10) COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY]
    GO全部文件代码using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;public partial class Drp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("select * from test where bianhao='"+DropDownList1.SelectedValue+"'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "name";
            DropDownList2.DataValueField = "name";
            DropDownList2.DataBind();
          
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strConn = "data source=(local);uid=sa;pwd=sundun;database=tempdb";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("select * from test where name='" + DropDownList2.SelectedValue + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);        DropDownList3.DataSource = ds.Tables[0];        DropDownList3.DataTextField = "address";
            DropDownList3.DataValueField = "address";
            DropDownList3.DataBind();
            }
    }客户端代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Drp.aspx.cs" Inherits="Drp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
            </atlas:ScriptManager>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>01</asp:ListItem>
                <asp:ListItem>02</asp:ListItem>
                <asp:ListItem>03</asp:ListItem>
            </asp:DropDownList>
            <atlas:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
                    </asp:DropDownList>&nbsp;
                </ContentTemplate>
                <Triggers>
                    <atlas:ControlValueTrigger ControlID="DropDownList1" PropertyName="SelectedValue" />
                </Triggers>
            </atlas:UpdatePanel>
            <atlas:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
            <asp:DropDownList ID="DropDownList3" runat="server">
            </asp:DropDownList>
                </ContentTemplate>
                <Triggers>
                    <atlas:ControlValueTrigger ControlID="UpdatePanel1" PropertyName="Triggers" />
                </Triggers>
            </atlas:UpdatePanel>
        <div>
            &nbsp;
        
        </div>
            &nbsp; &nbsp;
        </form>
    </body>
      

  9.   

    吧dropdowlist的AutoPostBack="True" 
    然后在onselectedindexchanged事件里面写你要实现的东西就行了
      

  10.   

    更正下9楼的说法,Atlas早就退出历史舞台了啊,哈哈
    现在就叫 Ajax不用那个名字了啊不过9楼的方法可以,但是和lz说的动态生成还是有点区别的!!
      

  11.   


    这种方法,我觉得有问题,他页面加载的时候就要查询数据库,然后绑定下拉列表,比如默认选项是第1项
    加载之后比如要选择第3项,经过AutoPostBack=True回传,页面再次加载的时候又显示的是第一项,而不是第3项。怎么办?
      

  12.   

    放在AjaxPanel中?
    我的程序里边用的AjaxPanel
    如果想只用AutoPostBack=True和onselectedindexchanged,不用其他控件,怎么搞?
      

  13.   

    动态添加dropdownlist或select同多文件上传
      

  14.   

    可以用webservces +ajax 很容易实现的