tomb<blog>

musings from the dark side

Jun 25, 2009

ASP.Net DropDownList SelectedIndex Extension

Jun 25, 2009

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.

 
Powered by Mango Blog All Content and Media © Copyright MMX
Tomb Multimedia Productions Ltd
All Rights Reserved