asp.net下拉列表findbytext [英] asp.net dropdown list findbytext
问题描述
我正在使用以下内容选择下拉列表,从列表中选择一个项目:
I am using the following to select have the dropdown list select an item from the list:
ddlIndustry.Items.FindByText("Trucking").Selected = true;
是否有另一种逻辑可以做到这一点.
Is there another logic for doing this.
我注意到我是否这样做:
I noticed if I do:
ddlIndustry.Items.FindByText("Trucking").Selected = true;
然后在代码中执行以下操作:
and then down the code do something like:
ddlIndustry.Items.FindByText("Cards").Selected = true;
我收到一条错误消息,说无法选择多个项目.
I get an error saying cannot select multiple items.
推荐答案
这是您要执行的操作:
ddlIndustry.SelectedValue = ddlIndustry.Items.FindByText("Cards").Value;
问题在于将 ListItem
设置为 Selected
不会清除对其他ListItem的选择.请记住, Items
属性是一个 ListItemColletion
,它也用在 ListBox
和 CheckListBox
中,它们允许多个项目选择(而 DropDownList
不允许这样做,这就是您收到错误的原因).
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).
使用 DropDownList
的 SelectedValue
属性可以为您进行多项选择,取消选择以前选择的项目,然后按值选择新项目.
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.
您可以在此处检查相关问题: https://stackoverflow.com/a/16068632/570191
You can check for a correlated issue here: https://stackoverflow.com/a/16068632/570191
这篇关于asp.net下拉列表findbytext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!