开发者

i have a problem with dropdownLists

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 se开发者_如何转开发lect 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


// add this line - it's different from ClearSelection()
SubTopicDropDownList.Items.Clear();

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


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


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);
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜