Поделиться через


DropDownList Web Server Control Overview

The DropDownList Web server control enables users to select a single item from a predefined drop-down list.

This topic contains:

  • Features

  • Background

  • Code Examples

  • Class Reference

Features

You can use the DropDownList control to:

  • Use data-binding to specify the list of items to display.

  • Determine which item is selected.

  • Specify the selected item programmatically.

Back to top

Background

The DropDownList Web server control enables users to select an item from a predefined list. It differs from the ListBox Web server control in that the list of items remains hidden until users click the drop-down button. In addition, the DropDownList control does not support multi-selection mode.

Modifying the Appearance of the DropDownList Control

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 instead.

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 DropDownList control. For details, see ASP.NET Web Server Controls and CSS Styles.

List Items

The DropDownList control is actually a container for the list items, which are of type ListItem. Each ListItem object is a separate object with its own properties. These properties are described in the following table.

Property

Description

Text

Specifies the text that is displayed in the list.

Value

Contains the value that is associated with an item. Setting this property enables 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 color and the Value property to its hexadecimal representation.

Selected

Indicates whether the item is currently selected.

To work with list items programmatically, use the Items collection of the DropDownList control. The Items collection is a standard collection, and you can add item objects to it, delete items, clear the collection, and so on.

The currently selected item is available in the DropDownList control's SelectedItem property.

Binding Data to the Control

You can use a DropDownList Web server control to list options that are made available to the page using a data source control. Each item in the DropDownList control corresponds to an item in the data source, typically an individual data record.

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 is not displayed.

As with other Web server controls, you can bind any control properties to data, such as the color or size of the control. For details, see How to: Populate List Web Server Controls from a Data Source.

The DropDownList control raises the SelectedIndexChanged event when users select an item. By default, this event does not cause the page to be posted to the server. However, you can cause the control to force an immediate post by setting the AutoPostBack property to true.

Note

The auto-postback capability requires that the browser support ECMAScript (JScript, or JavaScript) and that scripting be enabled on the user's browser.

Back to top

Code Examples

How to: Add DropDownList Web Server Controls to a Web Forms Page

How to: Determine the Selection in List Web Server Controls

How to: Respond to Changes in List Web Server Controls

How to: Add Items in List Web Server Controls

How to: Populate List Web Server Controls from a Data Source

How to: Set the Selection in List Web Server Controls

Back to top

Class Reference

The following table lists the classes that relate to the DropDownList control.

Member

Description

DropDownList

The main class for the control.

ListItem

The class that represents each item in the drop-down list.

Back to top

See Also

Reference

ListBox Web Server Control Overview