David Bonnici David Bonnici - 2 months ago 3x
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;