我的下拉列表有问题
我有一本看起来像这样的字典 Dictionary
我还有 2 个下拉列表。我希望第二个下拉列表显示数据,具体取决于第一个下拉列表中选择的项目。
所以我向第二个下拉列表添加了一个事件..这是算法:
protected void topicDropDownMenu_SelectedIndexChanged1(object sender, EventArgs e)
{
string[] chosenItem;
chosenItem = null;
SubTopicDropDownList.ClearSelection();
chosenItem = topic[topicDropDownMenu.SelectedItem.Value];
foreach (string item in chosenItem)
{
SubTopicDropDownList.Items.Add(item);
}
}
实际发生的情况是,每次我从第一个下拉列表中选择一个项目时,都会将一个字符串数组添加到第二个下拉列表中..
但我想要第二个下拉列表根据第一个下拉列表中选择的内容替换其值,而不是将这些值添加到第二个下拉列表中已放置的值
I have a dictionary that looks like this Dictionary<string,string[]>
I also have 2 dropdownlist. I want the second dropdownlist present data depending on what the item from the first dropdropdownlist was chosen.
So i added an event to the second dropdownlist..here is the algorithm:
protected void topicDropDownMenu_SelectedIndexChanged1(object sender, EventArgs e)
{
string[] chosenItem;
chosenItem = null;
SubTopicDropDownList.ClearSelection();
chosenItem = topic[topicDropDownMenu.SelectedItem.Value];
foreach (string item in chosenItem)
{
SubTopicDropDownList.Items.Add(item);
}
}
what actually happens, is that an array of strings is added to the second dropdownlist each time i select an item from the first dropdownlist..
but i want the second dropdownlist to replace its values depending on what was chosen in the first dropdownlist, instead of adding those values to whatever was already placed in the second drop down list
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在重新加载之前清除列表中的项目?请参阅 ListItemCollection.Clear 方法。
Clear the items of the list before you reload it? See the ListItemCollection.Clear Method.
SubTopicDropDownList.ClearSelection()
不会清空列表,它只是取消选择所选项目。您可以通过在调用SubTopicDropDownList.ClearSelection( 之前和之后查看
。您真正想要做的是使用SelectedIndex
、SelectedItem
或SelectedValue
的值来确认这一点)SubTopicDropDownList.Items.Clear()
清空/清除整个项目集。所以正确的代码是:
SubTopicDropDownList.ClearSelection()
doesn't empty the list, it just deselects the selected item. You can confirm this by looking at the value ofSelectedIndex
,SelectedItem
orSelectedValue
before and after making the call toSubTopicDropDownList.ClearSelection()
. What you actually want to do is empty/clear the whole set of items, usingSubTopicDropDownList.Items.Clear()
.So the correct code would be: