asp.net dropdown list findbytext

后端 未结 2 605
眼角桃花
眼角桃花 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

    0 讨论(0)
  • 2021-01-15 17:18

    Try using ClearSelection to clear previous selection:

    ddlIndustry.ClearSelection();
    if (ddlIndustry.Items.FindByText("Cards") != null)
        ddlIndustry.Items.FindByText("Cards").Selected = true;
    
    0 讨论(0)
提交回复
热议问题