asp.net下拉列表findbytext [英] asp.net dropdown list findbytext

查看:85
本文介绍了asp.net下拉列表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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆