How to SELECT a dropdown list item by value programmatically

David Bonnici picture David Bonnici · Aug 8, 2009 · Viewed 274.9k times · Source

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

Answer

ScottE picture ScottE · Aug 8, 2009

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;
}