How to SELECT a drop down list item by value programatically in C#.NET?
Ian Boyd (above) had a great answer -- Add this to Ian Boyd's class "WebExtensions" to select an item in a dropdownlist based on text:
///
/// Selects the item in the list control that contains the specified text, if it exists.
///
///
/// The text of the item in the list control to select
/// Returns true if the value exists in the list control, false otherwise
public static Boolean SetSelectedText(this DropDownList dropDownList, String selectedText)
{
ListItem selectedListItem = dropDownList.Items.FindByText(selectedText);
if (selectedListItem != null)
{
selectedListItem.Selected = true;
return true;
}
else
return false;
}
To call it:
WebExtensions.SetSelectedText(MyDropDownList, "MyValue");