Pieter Brinkman Blog

Some tips for the Asp.net dropdown control

1 min readpieterASP.NetControls

I always have problems implementing the Asp.Net dropdown control. Here are some simple tips to make your life (with dropdown controls) easier. Adding an default item with data binded dropdown To add an default listItem to your data binded dropdown list you just have to add a ListItem on the first place (0) of your control

CountryDropdown.DataSource = countryList;
CountryDropdown.DataValueField = "CountryId";
CountryDropdown.DataTextField = "CountryName";
CountryDropdown.DataBind();
//Add the item CountryDropdown.Items.Inser(0, new ListItem("== Choose a Country =="));

Getting the selected value

CountryDropdown.Items\[CountryDropdown.SelectedIndex\].Text;

Setting dropdown listitem by value

dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(option.OptionValue));

Validation on a dropdownlist

Hope it helps.