添加项目到下拉列表不起作用
问题描述:
我在一个下拉列表中有一个奇怪的问题。
它有数据绑定项目并依赖于另一个下拉列表。
因此,如果我从第一个下拉列表中选择一个值,那么这个下拉列表将包含数据绑定项。
我的问题我有的是我想在索引0处添加一个值为ALL的值,并在显示数据绑定项后显示但这不起作用。我有它在这个下面的其他下拉菜单工作。
这里是我的代码:
Hi,
I have a weird issue on one drop down list.
It has databound items and is dependant on another dropdownlist.
So if I select a value from the 1st dropdown list then this dropdownlist in question will have databound items.
the issue that I am having is that I want to add a value at index 0 that says "ALL" and there after display the databound items but this is not working. I have it working for the rest of the dropdowns beside this 1.
here is my code:
ddlPromoSuffEdit.Items.Clear();
ddlPromoSuffEdit.Items.Add(new ListItem("All", "")); //this is not being added in but data items are populated
ddlPromoSuffEdit.DataSourceID = "";
ddlPromoSuffEdit.DataSource = odsPromoSuffEdit.Select();
ddlPromoSuffEdit.DataBind();
答
如果在插入新的ListItem之前你的DataBind()你的列表你应该会发现它会按要求显示。
这样的东西应该有效:
Hi,
If you DataBind() your list before inserting the new ListItem you should find it will display as requested.
Something like this should work:
ddlPromoSuffEdit.Items.Clear();
ddlPromoSuffEdit.DataSourceID = "";
ddlPromoSuffEdit.DataSource = odsPromoSuffEdit.Select();
ddlPromoSuffEdit.DataBind();
//Insert new ListItem ...
ddlPromoSuffEdit.Items.Insert(0, (new ListItem("All", "")));
...希望它有所帮助。
... hope it helps.