DropDownList不正确的SelectedIndex

时间:2016-07-29 18:30:21

标签: c# asp.net dropdown

DropDownList控件正在返回错误的项目。

我的代码在SelectedItem时使用DropDownList.SelectedValue = -1执行特定操作。

在测试代码时,我会在A选择项index = 2。结果DropDownList.SelectedItem = "A"。好到目前为止。

但我在B选择了index = 3项。结果DropDownList.SelectedItem = "A"。根据调试器,在两种情况下都是DropDownList.SelectedIndex = 2

是什么原因导致SelectedIndex始终采用相同的值?

注意:当我通过C选择项Z时,他们会返回正确的SelectedTextSelectedIndex

if(Int32.Parse(dropdown.SelectedValue).Equals(-1))
{
    // Do something with the selected item
    DoThis(dropdown.SelectedItem.ToString());
}

1 个答案:

答案 0 :(得分:3)

当DropDownList的多个项目具有相同的值时,选择其中任何一个项目都会变得好像选择了具有该值的第一个项目。您应该修改重复值以使每个值都是唯一的。