//主要代码
//下面就是实现的功能就是选择专业就可以显示学生姓名了,其中属性设置了:如下!
//<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();
}
}
//可惜就是实现不了,谢谢!!
//下面就是实现的功能就是选择专业就可以显示学生姓名了,其中属性设置了:如下!
//<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();
}
}
//可惜就是实现不了,谢谢!!
这是我的运行效果,就是listbox绑定不了!
就是只有listbox不能显示,第一个还是可以的!
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();
while(reader.Read())
{
//循环绑定或使用dataset,datatable试试
}
给listbox的绑定语句换成这样试试
在看看你的SQL语句错了没
谢谢啦!