Controls You Can Use on Web Forms ASP.NET Standard Controls DropDownList Control
The DropDownList Web server control allows users to select a single item from a predefined list.
It is similar in function to controls that derive from the base ListControl class, particularly the ListBox Web server control, except that the list of items remains hidden until users click the drop-down button. In addition, the DropDownList control differs from the ListBox control in that it does not support multi-selection mode.
The DropDownList control is actually a container for a list of items, which are of type Listitem. Each ListItem is a separate object with its own properties, as described in the following table.
Property |
Description |
Text |
The text displayed in the list. |
Value |
The value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it. For example, you can set the Text property to the name of a U.S. state and the Value property to its postal abbreviation. |
Selected |
A Boolean value indicating whether the item is selected. |
The items in a DropDownList Web server control can be set in the following ways:
The currently selected item is available via the DropDownList control’s SelectedItem property.
The DropDownList Web server control can be used to list options that are read from a data source. The DropDownList control provides the following options for binding to data:
- Data binding using the DataSourceID property, which allows binding the DropDownList control to a data source control.
- Data binding using the DataSource property, which allows binding to any data source that implements the ICollection or IEnumerable interface, including ADO.NET datatables and datareaders. This approach, though, requires some coding.
When binding data, each item in the DropDownList control corresponds to an item — typically a row — in the data source.
The control displays one field from the source. Optionally, you can bind the control to a second field to set the value of an item, which does not display.
As with other server controls, you can bind any control properties, such as the color or size of the control, to data.
For details, see Populating a List Control from a Database.
The DropDownList control raises the SelectedIndexChanged when users select an item, or more precisely, when users select an item other than the currently selected item. By default, this event does not cause the page to be posted to the server, but you can cause the control to force an immediate post by setting the control’s AutoPostBack property to true.
NOTE: The ability of a DropDownList to automatically post to the server when clicked requires that the browser support ECMAScript and that scripting is enabled on the user’s browser.
You can control the look of the DropDownList control by setting its height and width in pixels. Some browsers do not support setting the height and width in pixels and will use the row count setting.
You cannot specify the number of items that are displayed in the list when users click the drop-down button. The length of the displayed list is determined by the browser.
As with other Web server controls, you can use style objects to specify the appearance of the control. For details, see Web Forms Server Controls and CSS Styles.
Adding DropDownList Controls to a Web Forms Page