Nevin Mathai
Nevin Mathai

Reputation: 2396

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

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

SomeDropDownList.DataSource =GetSomeStrings();

Upvotes: 3

Views: 39917

Answers (3)

alejofv
alejofv

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

Ariel
Ariel

Reputation: 4510

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.

Upvotes: 3

SLaks
SLaks

Reputation: 888283

Set the SelectedValue property

Upvotes: 2

Related Questions