An extension to quickly and easily set your DropDowns while avoiding errors
Sometimes you want to set your DropDownList to a value that may or may *not* be in the DropDownList, but setting that can be a bit tricky as it will often error out.
This extension will avoid that and give you a simple way to set your DropDownLists wihtout all the extra code:
public static void Set(this DropDownList ddl, string findByVal)
{ // attempts to set a DDL to the 'findByVal'
try { ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(findByVal)); }
catch { };
}
Now just call it from your DropDownList:
SomeDropDownList.Set("Some String Value");
And it will attempt to set your DropDownList to that value, but *not* error out if it doesn't exist.