The DropDownList Web Control is one of the many built-in controls in ASP.NET. This control allows a user to select an item from the drop-down list.
You can set the values of the drop-down list in multiple ways, I will show you how to do it declaratively in your ASPX page.
Webcontrols are declared using tags similar to HTML tags. The tags begin with ASP and are followed by a colon which is then followed by the name of the webcontrol ( DropDownList, Label, Literal, Calender etc.).
So the drop-down list in this example will do the following: It will display a list of car manufactures, pre-selecting an item from the list that has the value of “Honda”. Keep in mind that there are two distinct ways you can pre-select an item. You can selected an item using the following methods:
FindByValue
FindByText
The FindByValue method will search in the value section of the resulting HTML while the FindByText searches for the value within the tags (my tag declarations don’t have any text in between the tags so it defaults to the same text as the value).
An important thing to notice in the code sample is the if statement. The if statement first checks if the page is a post back, if it is the first time you are viewing the page it will pre-select the list item with the value of “Honda”, otherwise it will selected the item you just selected and output the value.
I also demonstrate the use of a server-side button. The button contains an event. An event is an action that is responded to when the button is clicked. The click event is called the OnClick event. In the ASPX page you will notice that I tell the page to fire my method called ItemSelected_Click whenever the button is clicked. The method then goes on to simply output the value of the drop-down list item just selected.