那位大哥能给个c#中下拉列表联动的例子?

解决方案 »

  1.   

    从数据库里读出数据放到下拉列表里:
     public void drop1()
        {
            if (!IsPostBack)
            {
                //连接数据库            //向下拉列表1增添数据
                SqlCommand cmd = new SqlCommand("SELECT DISTINCT yqkfdw FROM jcjlb where sp='审批人'", conn);
                SqlDataReader mydr = cmd.ExecuteReader();
                while (mydr.Read())
                {
                    ListItem lt = new ListItem();
                    lt.Text = mydr[0].ToString().Trim();
                    lt.Value = mydr[0].ToString().Trim();
                    this.DropDownList3.Items.Add(lt);
                }
                mydr.Close();
                string yqkf = DropDownList3.SelectedValue;
                conn.Close();            //执行drop2,向下拉列表2中填数据
                this.drop2();
            }
        }
    public void drop2()
        {        //连接数据库,如果第一个下拉列表的值为“全部”:
                if (DropDownList3.Text == "全部")
                {
                    DropDownList4.Items.Clear();
                    ListItem lt4 = new ListItem();
                    lt4.Text = "全部";
                    this.DropDownList4.Items.Add(lt4);                SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT sgdw FROM jcjlb where sp='审批人'", conn1);
                    SqlDataReader dr1 = cmd1.ExecuteReader();
                    while (dr1.Read())
                        {                        ListItem lt1 = new ListItem();
                            lt1.Text = dr1[0].ToString();
                            lt1.Value = dr1[0].ToString();
                            this.DropDownList4.Items.Add(lt1);
                        }                                        dr1.Close();
                        conn1.Close();
                   
                }
                else
                {
                    
                        DropDownList4.Items.Clear();
                        string sg = DropDownList3.Text.Trim();
                        SqlCommand cmd2 = new SqlCommand("SELECT DISTINCT sgdw FROM jcjlb where yqkfdw='" + sg + "'and sp='审批人'", conn1);
                        SqlDataReader dr2 = cmd2.ExecuteReader();
                        while (dr2.Read())
                        {                        ListItem lt2 = new ListItem();
                            lt2.Text = dr2[0].ToString().Trim();
                            lt2.Value = dr2[0].ToString().Trim();
                            this.DropDownList4.Items.Add(lt2);
                        }                    ListItem lt3 = new ListItem();
                        lt3.Text = "全部";
                        this.DropDownList4.Items.Add(lt3);                    dr2.Close();
                        conn1.Close();
                    }
                
            
        } protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drop2();
        }drop1放到page_load里面,dropdownlist3是第一个下拉菜单这是我从网上搜来自己改了一点无关紧要的东西,用的还可以,要是着急的话可以先这么用着,还有N多无刷新的也可以找找