Reputation: 2396
How do I set an item in a DropDownList as the default value in ASP.NET?
SomeDropDownList.DataSource =GetSomeStrings();
Upvotes: 3
Views: 39917
Reputation: 411
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;
Upvotes: 1
Reputation: 4510
There are two ways to do it:
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
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.
Upvotes: 3