我的下拉列表有问题

发布于 2024-11-06 13:07:28 字数 659 浏览 0 评论 0原文

我有一本看起来像这样的字典 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悸初 2024-11-13 13:07:28
// add this line - it's different from ClearSelection()
SubTopicDropDownList.Items.Clear();

foreach (string item in chosenItem)
{
    SubTopicDropDownList.Items.Add(item);
}
// add this line - it's different from ClearSelection()
SubTopicDropDownList.Items.Clear();

foreach (string item in chosenItem)
{
    SubTopicDropDownList.Items.Add(item);
}
苯莒 2024-11-13 13:07:28

在重新加载之前清除列表中的项目?请参阅 ListItemCollection.Clear 方法

Clear the items of the list before you reload it? See the ListItemCollection.Clear Method.

树深时见影 2024-11-13 13:07:28

SubTopicDropDownList.ClearSelection() 不会清空列表,它只是取消选择所选项目。您可以通过在调用 SubTopicDropDownList.ClearSelection( 之前和之后查看 SelectedIndexSelectedItemSelectedValue 的值来确认这一点)。您真正想要做的是使用 SubTopicDropDownList.Items.Clear() 清空/清除整个项目集。

所以正确的代码是:

protected void topicDropDownMenu_SelectedIndexChanged1(object sender, EventArgs e)
{
    string[] chosenItem;
    chosenItem = null;
    ubTopicDropDownList.Items.Clear();

    chosenItem = topic[topicDropDownMenu.SelectedItem.Value];

    foreach (string item in chosenItem)
    {
        SubTopicDropDownList.Items.Add(item);
    }

}

SubTopicDropDownList.ClearSelection() doesn't empty the list, it just deselects the selected item. You can confirm this by looking at the value of SelectedIndex, SelectedItem or SelectedValue before and after making the call to SubTopicDropDownList.ClearSelection(). What you actually want to do is empty/clear the whole set of items, using SubTopicDropDownList.Items.Clear().

So the correct code would be:

protected void topicDropDownMenu_SelectedIndexChanged1(object sender, EventArgs e)
{
    string[] chosenItem;
    chosenItem = null;
    ubTopicDropDownList.Items.Clear();

    chosenItem = topic[topicDropDownMenu.SelectedItem.Value];

    foreach (string item in chosenItem)
    {
        SubTopicDropDownList.Items.Add(item);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文