Some tips for the Asp.net dropdown control

by Pieter Brinkman 30. December 2008 04:41

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

[code:c#]

CountryDropdown.DataSource = countryList;
CountryDropdown.DataValueField = "CountryId";
CountryDropdown.DataTextField = "CountryName";
CountryDropdown.DataBind();

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

[/code]


Getting the selected value

[code:c#]

CountryDropdown.Items[CountryDropdown.SelectedIndex].Text;

[/code]


Setting dropdown listitem by value

[code:c#]

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

[/code]

 

Validation on a dropdownlist

[code:c#]

<asp:RequiredFieldValidator id="reqGroupDDL"
        Text="Choose group"
        InitialValue="selectGroup"
        ControlToValidate="GroupDDL"
        Runat="server" />

[/code]


Hope it helps.

Tags: , , ,

ASP.Net | Controls

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am Solution Architect for Sitecore in The Netherlands. My interests are mainly ASP.NET, MSSQL and Content Management Systems.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS

Most comments