asp.net dropdown list findbytext

后端 未结 2 606
眼角桃花
眼角桃花 2021-01-15 16:39

I am using the following to select have the dropdown list select an item from the list:

    ddlIndustry.Items.FindByText(\"Trucking\").Selected = true;
         


        
2条回答
  •  一整个雨季
    2021-01-15 17:05

    This is what you want to do:

    ddlIndustry.SelectedValue = ddlIndustry.Items.FindByText("Cards").Value;
    

    The problem is that making ListItem as Selected does not clear selection of other ListItems. Keep in mind that Items property is a ListItemColletion, which is also used in ListBox and CheckListBox, which allow multiple item selection (while DropDownList does not allow that, which is why you got the error).

    Using the SelectedValue propery of the DropDownList takes care of the multi-selection for you, unselecting previously selected items and selecting the new item by value.

    You can check for a correlated issue here: https://stackoverflow.com/a/16068632/570191

提交回复
热议问题