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

Amana said:

Hello. Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. Help me! I find sites on the topic: Quartz clock. I found only this - <a href="wall-clock.biz/.../">double faced wall clock</a>. Wall clock, the slaves left they added their smoking in dangerous autumn. Wall clock, the standards vanished were song, richness, rebuilding and life decision. Waiting for a reply :eek:, Amana from Kenya.

# March 25, 2010 12:58 AM

kikus said:

интеретсный блог почему только так мало читателей на нём

# June 13, 2010 7:12 PM

DropDownList select item by value – Gotit DevBlog said:

Pingback from  DropDownList select item by value &#8211; Gotit DevBlog

# September 10, 2010 8:22 AM

Mounir NAJAHI said:

thanks,

I used:

foreach (ListItem item in this.DropDownList1.Items)

      {

          item.Selected = false;

      }

then

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

it works !!

# October 7, 2010 5:32 AM

df said:

you could also:

DropDownList1.SelectedItem.Selected = False

DropDownList1.Items.FindByValue(MyValue).Selected = True

# January 5, 2011 4:32 PM

MB said:

Hi Atilla,

u can use ClearSelection() property of the dropdownlist control and then assign the  Selected=True value to it.

It will work with charm...

# April 11, 2011 5:47 AM

Ruholla jafari said:

better solution could be :

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(strProductCode));

# May 3, 2011 5:28 PM

IVan said:

Can you make example of selecting default value from dropdownlist which is column in ultragrid?

# June 20, 2011 3:52 AM

Pupeevetlylit said:

Доброго времени суток,  

Хочу представить вам прозелит лавка курительных смесей

сайт магазина http://spice-family.ru  

3г микса Weaken - 1,500 р. + доставка (ems, pony get across)  

Сообразно вопросам опта писать оазисами в скайп - FomaX2

# September 3, 2011 12:46 AM

Ravi Kumar said:

I want to run if Condition if RBtStock.Checked other wise run else statement.... But it is not working

please assist me ---

protected void RBtStock_CheckedChanged(object sender, EventArgs e)

   {

       if (RBtStock.Checked)

       {

           Lblmsg.Text = "Stock";

           RBtIssue.Checked = false;

       }

       else

       {

           Lblmsg.Text = "Issue";

           RBtStock.Checked = true;

       }

   }

# January 27, 2012 7:20 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)