C# – How to SELECT a dropdown list item by value programmatically

cdrop-down-menunet

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

Best Answer

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