public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlConn"]);
        SqlConnection myconn = new SqlConnection(settings);
        myconn.Open();
        string strsql = "select csmc from Table_1";
        SqlCommand cm = new SqlCommand(strsql, myconn);
        SqlDataReader dr = cm.ExecuteReader();
        while (dr.Read())
        {
            DropDownList1.Items.Add(new ListItem(dr["csmc"].ToString()));
        }
        myconn.Close();
    }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
                                                        
        string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlConn"]);
        SqlConnection myconn = new SqlConnection(settings);
        myconn.Open();
        string strsql = "select xqmc from Table_2 where csmc=(select csmc from Table_1 where csmc = DropDownList1.SelectedValue)";
        SqlCommand cm = new SqlCommand(strsql, myconn);
        SqlDataReader dr = cm.ExecuteReader();
        while (dr.Read())
        {
            DropDownList2.Items.Add(new ListItem(dr["xqmc"].ToString()));
        }
        myconn.Close();
    }
}

解决方案 »

  1.   

    protected void Page_Load(object sender, EventArgs e)
        {
            string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlConn"]);
            SqlConnection con = new SqlConnection(settings);
            con.Open();
            SqlCommand scd = new SqlCommand("select * from Table_1", con);
            SqlDataReader sdr = scd.ExecuteReader();
            this.DropDownList1.DataSource = sdr;
            this.DropDownList1.DataTextField = "csmc";
            this.DropDownList1.DataValueField = "csmc";
            this.DropDownList1.DataBind();
            sdr.Close();        SqlCommand scd1 = new SqlCommand("select * from Table_2 where csmc='" + this.DropDownList1.SelectedValue + "'", con);
            SqlDataReader sdr1 = scd1.ExecuteReader();
            this.DropDownList2.DataSource = sdr1;
            this.DropDownList2.DataTextField = "xqmc";
            this.DropDownList2.DataValueField = "xqmc";
            this.DropDownList2.DataBind();
            sdr1.Close();
        }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Name = this.DropDownList1.SelectedValue;
            string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlConn"]);
            SqlConnection con = new SqlConnection(settings);
            con.Open();
            SqlCommand scd2 = new SqlCommand("select * from Table_2 where xqmc ='" + Name + "'", con);
            SqlDataReader sdr2 = scd2.ExecuteReader();
            this.DropDownList2.DataSource = sdr2;
            this.DropDownList2.DataTextField = "xqmc";
            this.DropDownList2.DataValueField = "xqmc";
            this.DropDownList2.DataBind();
            sdr2.Close();
            con.Close();
        }改成这样,只能显示第一个下拉中第一索引中的内容,郁闷,随便第一个下拉怎么变,第二个都不动
      

  2.   

    没看具体代码,提一点,你第一个下拉框里面是否设置了autopostback=true