C# 如何将下拉列表项设置为在 ASP.NET 中选择?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10974992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:59:34  来源:igfitidea点击:

How to set a dropdownlist item as selected in ASP.NET?

c#asp.net

提问by farouk

I want to set selecteditem for asp. net dropdownlist control programmatically.

我想为asp设置selecteditem。net dropdownlist 以编程方式控制。

So I want to pass a value to the dropdownlist control to set the selected item where is the value of the item equal to the passed value.

所以我想将一个值传递给下拉列表控件以设置所选项目,其中项目的值等于传递的值。

采纳答案by Raab

dropdownlist.ClearSelection(); //making sure the previous selection has been cleared
dropdownlist.Items.FindByValue(value).Selected = true;

回答by Adil

You can set the SelectedValueto the value you want to select. If you already have selected item then you should clear the selection otherwise you would get "Cannot have multiple items selected in a DropDownList" error.

您可以将 设置为SelectedValue您想要选择的值。如果您已经选择了项目,那么您应该清除选择,否则您会收到“无法在 DropDownList 中选择多个项目”错误。

dropdownlist.ClearSelection();
dropdownlist.SelectedValue = value;

You can also use ListItemCollection.FindByTextor ListItemCollection.FindByValue

您还可以使用ListItemCollection.FindByTextListItemCollection.FindByValue

dropdownlist.ClearSelection();  
dropdownlist.Items.FindByValue(value).Selected = true;

Use the FindByValue method to search the collection for a ListItem with a Value property that contains value specified by the value parameter. This method performs a case-sensitive and culture-insensitive comparison. This method does not do partial searches or wildcard searches. If an item is not found in the collection using this criteria, null is returned, MSDN.

使用 FindByValue 方法在集合中搜索具有 Value 属性的 ListItem,该属性包含由 value 参数指定的值。此方法执行区分大小写和不区分区域性的比较。此方法不执行部分搜索或通配符搜索。如果使用此条件在集合中找不到项目,则返回 null,MSDN

If you expect that you may be looking for text/value that wont be present in DropDownListListItem collection then you must check if you get the ListItemobject or nullfrom FindByTextor FindByValuebefore you access Selected property. If you try to access Selected when null is returned then you will get NullReferenceException.

如果您希望查找不会出现在DropDownListListItem 集合中的文本/值,那么您必须检查是否获取了ListItem对象nullFindByText或者FindByValue在访问 Selected 属性之前或之前检查。如果您在返回 null 时尝试访问 Selected ,那么您将获得NullReferenceException

ListItem listItem = dropdownlist.Items.FindByValue(value);

if(listItem != null) 
{
   dropdownlist.ClearSelection();
   listItem.Selected = true;
}

回答by Cacho Santa

This is a very nice and clean example:(check this great tutorial for a full explanation link)

这是一个非常漂亮和干净的例子:(查看这个很棒的教程以获得完整的解释链接

public static IEnumerable<SelectListItem> ToSelectListItems(
              this IEnumerable<Album> albums, int selectedId)
{
    return 
        albums.OrderBy(album => album.Name)
              .Select(album => 
                  new SelectListItem
                  {
                    Selected = (album.ID == selectedId),
                    Text = album.Name,
                    Value = album.ID.ToString()
                   });
}

In this MSDN linkyou can read de DropDownListmethod documentation.

在此 MSDN链接中,您可以阅读 deDropDownList方法文档。

Hope it helps.

希望能帮助到你。

回答by giftcv

You can use the FindByValue method to search the DropDownList for an Item with a Value matching the parameter.

您可以使用 FindByValue 方法在 DropDownList 中搜索值与参数匹配的项。

dropdownlist.ClearSelection();
dropdownlist.Items.FindByValue(value).Selected = true;

Alternatively you can use the FindByText method to search the DropDownList for an Item with Text matching the parameter.

或者,您可以使用 FindByText 方法在 DropDownList 中搜索文本与参数匹配的项目。

Before using the FindByValue method, don't forget to reset the DropDownList so that no items are selected by using the ClearSelection() method. It clears out the list selection and sets the Selected property of all items to false. Otherwise you will get the following exception.

在使用 FindByValue 方法之前,不要忘记重置 DropDownList 以便使用 ClearSelection() 方法不会选择任何项目。它清除列表选择并将所有项目的 Selected 属性设置为 false。否则,您将收到以下异常。

"Cannot have multiple items selected in a DropDownList"

回答by Mac

Set dropdown property

设置下拉属性

selected="true"