共用方式為


DropDownList Web Server Control Overview

The DropDownList Web server control allows 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 differs from the ListBox control in that it 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 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

Indicates whether the item is selected by means of a Boolean.

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 — 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 Web server controls, you can bind any control properties, such as the color or size of the control, to data. For details, see How to: Populate List Web Server Controls from a Data Source.

The DropDownList control raises an event — the SelectedIndexChanged event — when users select an 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 AutoPostBack property to true.

NoteNote

The ability of a DropDownList control to post to the server when it is checked requires that the browser support ECMAScript (JScript, or JavaScript) and that scripting be enabled on the user's browser.

See Also

Tasks

How to: Add Items in List Web Server Controls
How to: Determine the Selection in List Web Server Controls
How to: Populate List Web Server Controls from a Data Source
How to: Respond to Changes in List Web Server Controls
How to: Set the Selection in List Web Server Controls