David Bonnici David Bonnici - 1 year ago 89
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?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download