我有两个下拉列表,第一个有四个选项,用户选择并基于此,第二个下拉列表将从选定的数据库列数据中填充。我试图来填充这些列表编程,而不是通过数据源绑定他们根据以前的下拉列表选择填充下拉列表
我目前拥有的代码有两个问题:
1.在我的“onselectedIndex改变”的方法,如果我不能引起我的活动我希望单击下拉列表中的第一个元素,我必须单击另一个项目,然后重新单击第一个元素,以便它也触发。我必须的AutoPostBack设置为true
2.当事件触发确实数据库中的数据不填充第二下拉列表,它是用正确的数据集的正确数量的行填充,但不显示数据。
一旦第一个元素是从第一个下拉列表,即“咖啡名称”,那么第二个列表中填入正确的行号选择,即3,但不正确的数据
当前代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string option = DropDownList1.SelectedValue;
// If the selected element is 'Coffee Name' run the method.
if (option == "Coffee Name")
{
bindCoffeeNames();
}
}
private void bindCoffeeNames()
{
Query the DB and bind the data to the second dropdown list.
SqlConnection sqlcon = new SqlConnection(connstring);
SqlCommand sqlcmd = new SqlCommand("select coffeeName from Coffees ORDER BY coffeeName ASC", sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
DropDownList2.DataSource = ds.Tables[0];
DropDownList2.DataBind();
}
非常感谢我工作的占位符'空白'默认项目。但数据字段完美运行。再次谢谢你。我会尽可能地标记你的答案 – user1352057 2013-03-21 00:45:16