views:

55

answers:

3

How do I set an item in a DropDownList as the default value in ASP.NET?

SomeDropDownList.DataSource =GetSomeStrings();
+1  A: 

Set the SelectedValue property

SLaks
A: 

You can bind the DropDownList to the data source, and then set the SelectedValue:

someDropDownList.DataSource = GetSomeStrings();
someDropDownList.DataBind();
someDropDownList.SelectedValue = "default value";

You could also select the default item by index, using the SelectedIndex property:

someDropDownList.SelectedIndex = 0;
alexphi
A: 

There are two ways to do it:

  1. Set the SelectedValue to the value you want as the default DropDownList.SelectedValue = "value". This is very simple but will result in an error if the dropdown already has a SelectedValue

  2. Set the actual item DropDOwnList.Items.FindByValue("value").Selected = true; which should not result in an error in case there already is a selected item.

Ariel