System.Web.HttpException: Cannot have multiple items selected in a DropDownList

后端 未结 3 599
我在风中等你
我在风中等你 2021-02-03 11:55

During page load, index 0 was already selected. Then this code statement selected index 1:

dropDownList.Items.FindByValue(myValue).Selected = true; 
// assume my         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-03 12:33

    I noticed that both index 0 and index 1 had the properties "Selected" set to true (dropDownList.Items[0].Selected and dropDownList.Items[1].Selected both were true). However, dropDownList.SelectedIndex was still 0, even though index 1 was set most recently.

    I tried resolving this by clearing the list selection beforehand.

    dropDownList.ClearSelection();
    dropDownList.Items.FindByValue(myValue).Selected = true;
    

    But that didn't help. Same exception occurred.

    What did help, was setting the selected value another way:

    dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(myValue));
    

    Now the selection change propogates throughout the list.

    So, don't use dropDownList.Items[x].Selected = true/false to change the selected value of a DropDownList. Instead, use dropDownList.SelectedIndex = x;

提交回复
热议问题