Samer Ibrahim's Blog

The Samer I Warrior on battles with .NET

Sponsors

Lists/Forums/Etc.

Which Blogs do I read?

Selecting an item in a DropDownList by value

Wow, this drove me insane today.  You would think there would be an easier way but I don't know of any.  We were trying to select an item in a DropDownList by it's value rather than by it's index because the index isn't known most of the time, ie when you get a DataSet from the database to populate the DropDownList.  So here it is:

DropDownList1.Items.FindByValue(MyValue).Selected = true;

I hope that helps any of you trying to do the same thing.  If anyone knows of a better way let me in on your secret.

Comments

Cads said:

thanks for this literally just read it now. I am having similar eek with this problem too. such an arse about face way of doing it.

# May 18, 2007 11:16 AM

jwest said:

Thank You, I wasted 10 minutes then jumped straight here.  So I got lucky this time. Thanks again.

# April 28, 2008 3:37 PM

tulika said:

thanks....

you 've saved me.

# May 29, 2008 10:25 AM

Nina said:

Thanks! I'm glad I decided to look it up after trying for 5 min!

# June 20, 2008 12:22 AM

Atilla said:

Hi all,

after I use

DropDownList1.Items.FindByValue(MyValue).Selected = true;

the i got the run time error.

two items selected in the dropwonlist

how could i solve this problem?

# July 2, 2008 3:02 PM

amrelgarhy said:

@Atilla

Just make sure that all items in the dropdown is Select=false before make the one you want true.

You can loop through them or something

foreach (ListItem item in this.DropDownList1.Items)

       {

           item.Selected = false;

       }

# March 29, 2009 10:41 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)