Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

How to set a SelectedItem in a ASP:DropDownList

  Asked By: David    Date: Jul 10    Category: Asp.net    Views: 2154
  

How can I properly set an item as a default (selected) in a
dropdownlist.
I did the following but it just replaces the first item
(SelectedIndex=0) in the list. So now I have a dropdown list that
has one missing one country and two 'USA'.


Country.SelectedItem.Text='USA'
Country.selectedItem.Value='USA'


I know if USA's index is 200 then I can set
Country.SelectedIndex = 200

But the country is dynamic.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Isaac Evans     Answered On: Jul 10

Are you looking for this:


Country.Items.FindByValue("USA").Selected = true

 
Answer #2    Answered By: Erin Dunn     Answered On: Jul 10

This is exactly what I was looking for.
As an alternative, which is a bit more complicated, I found the
following example that does the same thing.

Country.SelectedIndex = Country.Items.IndexOf
(Country.Items.FindByValue("USA"))

THANK YOU again. This is a GREAT place for ASP.NET coders.

 
Didn't find what you were looking for? Find more on How to set a SelectedItem in a ASP:DropDownList Or get search suggestion and latest updates.




Tagged: