Some tips for the dropdown control

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";

//Add the item
CountryDropdown.Items.Inser(0, new ListItem("== Choose a Country =="));

Getting the selected value


Setting dropdown listitem by value

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

Validation on a dropdownlist

Hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *