日期:2014-05-17  浏览次数:20551 次

DropDownList控件绑定数据库后,下拉列表没有内容??!!
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {

  if (!Page.IsPostBack)
  {
  Message get = new Message();
  DataSet set = get.GetMessage();
  this.DropDownList1.DataSource = set;
  this.DropDownList1.DataValueField = "ID";
  this.DropDownList1.DataTextField = "Title";
  this.DropDownList1.DataBind();
  }
  }

-----------------------------------------------------------------
 public DataSet GetMessage()
  {
  string connectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
  SqlConnection con = new SqlConnection(connectionString);

  string cmdText = "select Title,ID from Message";
  SqlDataAdapter sda = new SqlDataAdapter(cmdText,con);
  DataSet ds = new DataSet();
  try
  {
  con.Open();
  sda.Fill(ds);
  }
  finally
  {
  con.Close();
  }
  return ds;
  }


调试时,DropDownList下拉列表没有内容,请各位高手指点!!(数据库连接是没有问题的。)

------解决方案--------------------
1.我想问,你加载的时候下拉根本没有绑定任何东西,那么 DropDownList1_SelectedIndexChanged 这个有什么用,无语
这样写:
 protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)//首次加载时绑定
{
Message get = new Message();
DataSet set = get.GetMessage();
this.DropDownList1.DataSource = set;
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField = "Title";
this.DropDownList1.DataBind();
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

 //改变选择后,执行其他操作
}
------解决方案--------------------
if (!Page.IsPostBack)
{
Message get = new Message();
DataSet set = get.GetMessage();
this.DropDownList1.DataSource = set;
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataTextField = "Title";
this.DropDownList1.DataBind();

DropDownList1.Items.Insert(0, "请选择");

}