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

hao19851986 2012-08-11 02:30:48
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下拉列表没有内容,请各位高手指点!!(数据库连接是没有问题的。)
...全文
233 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovebaby 2012-08-13
  • 打赏
  • 举报
回复
DropDownList1_SelectedIndexChanged本身应该是回发时产生的吧,if (!Page.IsPostBack)是判断页面第一次加载执行,两者不矛盾吗?
xiaowang3266 2012-08-12
  • 打赏
  • 举报
回复
开源了,学习了!!!
春天的气息 2012-08-12
  • 打赏
  • 举报
回复

public static DataSet GetTableData(string sql, string tablename)
{


SqlConnection con = new SqlConnection(connString);
SqlDataAdapter Sdr = new SqlDataAdapter(sql, con);
con.Open();
DataSet ds = new DataSet();
Sdr.Fill(ds, tablename);
return ds;
con.Close();
}


GetTableData也给你,呵呵,可以了吧,上面的sql由你改,想怎样用就怎样用。
春天的气息 2012-08-12
  • 打赏
  • 举报
回复
给你一个我通用的绑定吧,字段,表自定义。

/// <summary>
/// 绑定dropdownlist字段方法
/// </summary>
/// <param name="ddname">dwopdownlist控件名称</param>
/// <param name="field">要绑定的字段</param>
/// <param name="tablename">表名</param>
public void BindDropDownList(DropDownList ddname, string field, string tablename, string typeid)
{
try
{

string sql = "select " + field + " from " + tablename + " where typeid='" + typeid + "'";
DataSet set = DBClass.GetTableData(sql, "sql");
ddname.DataSource = set;
ddname.DataTextField = field;
ddname.DataBind();
ddname.Items.Insert(0, new ListItem("请选择...", ""));

}
catch (Exception f)
{
error.alter(f.Message);
}
}



GetTableData 是生成dataset的小方法,
dusth 2012-08-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
在Default.aspx.cs中是这样的,正确吗?

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

BindDDL();

}
……
[/Quote]



为何非要
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindDDL();


}
牛哥_ 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

1.我想问,你加载的时候下拉根本没有绑定任何东西,那么 DropDownList1_SelectedIndexChanged 这个有什么用,无语
这样写:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//首次加载时绑定
{
Message get = new ……
[/Quote]


看这个 肯定行 在页面加载的时候 查询绑定 要不然怎么实现
hao19851986 2012-08-11
  • 打赏
  • 举报
回复
应该按6楼和8楼的方法来调试,可以了!谢谢!!!

现在还有一个问题,怎么在第一次加载时DropDownList1初始值为 “请选择” ???
杰拉尔 2012-08-11
  • 打赏
  • 举报
回复
在Default.aspx.cs中是这样的,正确吗?

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

BindDDL();

}



}
protected void BindDDL()
{
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)
{
BindDDL();


}
}
  • 打赏
  • 举报
回复
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();
}
放到pageload里试试
孫大聖 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

还是不行!
[/Quote]

你断点调试下DataSet里面有只吗??
我觉得你绑定的数据源应该是一个表。而不是DataSet
this.DropDownList1.DataSource = set.Tables[0];

dusth 2012-08-11
  • 打赏
  • 举报
回复
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, "请选择");

}

  • 打赏
  • 举报
回复
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)
{

//改变选择后,执行其他操作
}
hao19851986 2012-08-11
  • 打赏
  • 举报
回复
还是不行!
huli304 2012-08-11
  • 打赏
  • 举报
回复
在Default.aspx.cs中是这样的,正确吗?

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {

  if (!Page.IsPostBack)
  {
  Message get = new Message();
  DataSet set = get.GetMessage();
  this.DropDownList1.DataSource = set.Tables[0];
  this.DropDownList1.DataValueField = "ID";
  this.DropDownList1.DataTextField = "Title";
  this.DropDownList1.DataBind();
  }
  }
}
hao19851986 2012-08-11
  • 打赏
  • 举报
回复
在Default.aspx.cs中是这样的,正确吗?

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

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();
}
}
}
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
Message get = new Message();
DataSet set = get.GetMessage();
this.DropDownLi……
[/Quote]

这应该是在 page_load 或者 paeg_prerender 中的代码吧?

hao19851986 2012-08-11
  • 打赏
  • 举报
回复
补充:(Default.aspx文件)


<br />
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList>
<br />

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧