Est unusquisque faber ipsae suae fortunae
Est unusquisque faber ipsae suae fortunae
I hate having to always look up the safe method of assigning values to a drop down list in asp.net plus I think its a little verbose.
Here is the old way I've been doing things:
DropDown1.SelectedIndex = DropDown1.Items.IndexOf(DropDown1.Items.FindByValue(value));
And now here is my extension class:
public static int SelectedIndex(this DropDownList ddl, string key)
{
return ddl.Items.IndexOf(ddl.Items.FindByValue(key));
}
And the implementation is like this:
DropDown1.SelectedIndex = DropDown1.SelectedIndex(value));
I went further and set the index right in the extension. Less code is better if it reads the same.