//主要代码
//下面就是实现的功能就是选择专业就可以显示学生姓名了,其中属性设置了:如下!
//<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        Height="28px" Width="134px /">
//<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" SelectionMode="Multiple" Height="70px" Width="136px" />
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlConnection stucn = new SqlConnection("server=(local);database=bangding;User id=sa;password=;");
            stucn.Open();
            SqlCommand stucm = new SqlCommand("select distinct 专业 from band", stucn);
            SqlDataReader studr = stucm.ExecuteReader();
            this.DropDownList1.DataSource = studr;
            this.DropDownList1.DataTextField = "专业";
            this.DropDownList1.DataBind();
            studr.Close();
            stucn.Close();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection stucn = new SqlConnection("server=(local);database=bangding;User id=sa;password=;");
        stucn.Open();
        ListBox1.Items.Clear();
        SqlCommand stucm=new SqlCommand("select 姓名 from band where 专业='"+DropDownList1.SelectedItem.ToString()+"'",stucn);
        SqlDataReader studr=stucm.ExecuteReader();
        this.ListBox1.DataSource=studr;
        this.ListBox1.DataTextField="姓名";
        this.ListBox1.DataBind();
        studr.Close();
        stucn.Close();
    }
}
//可惜就是实现不了,谢谢!!

解决方案 »

  1.   

    是下拉框绑定不了还是ListBox呀??
      

  2.   


    这是我的运行效果,就是listbox绑定不了!
      

  3.   

    检查一下你的SQL语句看有没有问题,,设个断点取出SQL语句放到查询分析器里运行一下看!~
      

  4.   

    不会用查询分析器啊?测试连接成功了的!
    就是只有listbox不能显示,第一个还是可以的!
      

  5.   

    改成这样试一下:SqlConnection stucn = new SqlConnection("server=(local);database=bangding;User id=sa;password=;"); 
            stucn.Open(); 
    ListBox1.Items.Clear(); 
    DataSet ds=new DataSet();
    SqlDataAdapter adapter=new SqlDataAdapter("select 姓名 from band where 专业='"+DropDownList1.SelectedItem.ToString()+"'",stucn);
    adapter.Fill(ds);
    if(ds!=null)
    {
            this.ListBox1.DataSource=ds; 
            this.ListBox1.DataTextField="姓名";
            this.ListBox1.DataValueField="姓名"; 
            this.ListBox1.DataBind(); }
          studr.Close(); 
            stucn.Close(); 
      

  6.   

    SqlDataReader reader=stucm.ExecuteReader(); 
    while(reader.Read())
    {
    //循环绑定或使用dataset,datatable试试
    }
      

  7.   

    select 姓名 from band where 专业='"+DropDownList1.Selectedvalue+"'
    给listbox的绑定语句换成这样试试
      

  8.   

    使用select 姓名 from band where 专业='"+DropDownList1.SelectedValue.ToString()+"'
      

  9.   

    晕倒,怎么直接绑定SqlDataReader了,用SqlDataAdapter,dataset
      

  10.   

    少写了一个this.ListBox1.DataValueField="姓名"; 
    在看看你的SQL语句错了没
      

  11.   

    哦,找到问题了,谢谢大家了,!!在属性里面没有这句onselectedindexchanged="DropDownList1_SelectedIndexChanged"
    谢谢啦!