当前位置:编程学习 > C#/ASP.NET >>

为什么只会在选择企业的时候,显示企业的信息,选择学校的时候不显示

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind1();
        }
    }
    private void Bind1()
    {
        DropDownList2.Items.Clear();
        SqlConnection con =new SqlConnection(@"Data Source=20090523-1912\SQLEXPRESS;Initial Catalog=rsglzxt;Integrated Security=True");
        string str="select * from dbo.user_Company";
        SqlCommand comm=new SqlCommand(str,con);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(comm);
        DataSet ds = new DataSet();
        sda.Fill(ds,"dbo.user_Company");
        this.DropDownList2.DataSource=ds;
        this.DropDownList2.DataTextField = "企业名称";
        this.DropDownList2.DataValueField = "企业编号";
        this.DropDownList2.DataBind();
        ds.Dispose();
        con.Close();
        comm.Dispose();
        con.Dispose();
    }
    private void Bind2()
    {
        DropDownList2.Items.Clear();
        SqlConnection con = new SqlConnection(@"Data Source=20090523-1912\SQLEXPRESS;Initial Catalog=rsglzxt;Integrated Security=True");
        string str = "select * from dbo.user_School";
        SqlCommand comm = new SqlCommand(str, con);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(comm);
        DataSet ds = new DataSet();
        sda.Fill(ds, "dbo.user_School");
        this.DropDownList2.DataSource = ds;
        this.DropDownList2.DataTextField = "学校名称";
        this.DropDownList2.DataValueField = "学校编号";
        this.DropDownList2.DataBind();
        ds.Dispose();
        con.Close();
        comm.Dispose();
        con.Dispose();

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       if (DropDownList1.SelectedValue == "企业")
        {
            Bind1();
        }
            else 
        {
            if (DropDownList1.SelectedValue == "学校")
            {
                Bind2();
            }
        }
    }
为什么只会在选择企业的时候,显示企业的信息,选择学校的时候不显示 --------------------编程问答-------------------- bind2是否执行,单步跟踪
using(SqlConnection con = new SqlConnection(@"");
 {
string str = "select * from user_School";
  SqlCommand comm = new SqlCommand(str, con);
  con.Open();
  SqlDataAdapter sda = new SqlDataAdapter(comm);
  DataSet ds = new DataSet();
  sda.Fill(ds);
 ..

}
使用数据库操作类就可以了 --------------------编程问答-------------------- 自己单步调调吧
--------------------编程问答-------------------- 我按照你的样子,花了一个小时+调试,重新做了一遍,没有发现问题。
我不知道是不是你的SelectedIndexChange事件没有响应。

可以在Property里面,将DropDownList1的AutoPostPost设置为True,试一试? --------------------编程问答--------------------
if (!IsPostBack)
  {
    Bind1();
    Bind2();//加上这一句就OK了
  }

--------------------编程问答-------------------- 不好意思上面我看错了,所以回答了一个错误的答案,经过我仔细一想
你可能是没有填写Value所在导致用.SelecetedValue取不到控件的值
所以我建议你用下面的方法试下


 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
  if (DropDownList1.Items[DropDownList1.SelectedIndex].Text == "企业")//看清我改的了吗?
  {
  Bind1();
  }
  else  
  {
  if (DropDownList1.Items[DropDownList1.SelectedIndex].Text == "学校")//这里不要忘了改
  {
  Bind2();
  }
  }

--------------------编程问答-------------------- 楼上的正确.
还有一个弱弱的见议.
确保dbo.user_School有楼据.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,