首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DropDownList控件绑定数据库后,上拉列表没有内容?

2012-09-18 
DropDownList控件绑定数据库后,下拉列表没有内容??!!protected void DropDownList1_SelectedIndexChanged(

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, "请选择");

}

热点排行