David Bonnici David Bonnici - 8 months ago 29
C# Question

How to SELECT a dropdown list item by value programatically

How to SELECT a drop down list item by value programatically in C#.NET?


If you know that the dropdownlist contains the value you're looking to select, use:

ddl.SelectedValue = "2";

If you're not sure if the value exists, use (or you'll get a null reference exception):

ListItem selectedListItem = ddl.Items.FindByValue("2");

if (selectedListItem != null)
    selectedListItem.Selected = true;