How do I set an item in a DropDownList as the default value in ASP.NET?
SomeDropDownList.DataSource =GetSomeStrings();
How do I set an item in a DropDownList as the default value in ASP.NET?
SomeDropDownList.DataSource =GetSomeStrings();
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;
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.