This documentation is archived and is not being maintained.

HtmlSelect Class

Allows programmatic access to the HTML <select> element on the server.

Namespace:  System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

[ValidationPropertyAttribute(L"Value")]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class HtmlSelect : public HtmlContainerControl, 
	IPostBackDataHandler, IParserAccessor
<asp:HtmlSelect />

Use the HtmlSelect control to create a selection box. Specify item listings in the control by placing HTML <option> elements between the opening and closing <select> tags. Each item is represented by a System.Web.UI.WebControls::ListItem object. To specify the text that is displayed for each item in the control, you can set the ListItem::Text property of the item, or simply place text between the opening and closing <option> tags. By setting the ListItem::Value property of the item, you can associate a value with the item that is different from the text. To select an item from the list by default, set the ListItem::Selected property of the item to true.

You can control the appearance and behavior of the HtmlSelect control by setting the Size and Multiple properties. The Size property specifies the height (in rows) of the control. The Multiple property specifies whether more than one item can be concurrently selected in the HtmlSelect control.

By default, the HtmlSelect control is displayed as a drop-down list box. If you allow multiple selections (by setting the Multiple property to true) or specify a height greater than one row (by setting the Size property to a value greater than 1), the control is displayed as a list box.

To determine the selected item in a single-selection HtmlSelect control, use the SelectedIndex property to get the index of the selected item. You can then use this value to retrieve the item from the Items collection.

To determine the selected items from an HtmlSelect control that allows multiple selections, iterate through the Items collection and test the ListItem::Selected property of each item.

The HtmlSelect class provides two properties to support data binding. To bind data to any object that implements the System.Collections::IEnumerable interface (such as System.Data::DataView, System.Collections::ArrayList, and System.Collections::Hashtable), or the IListSource interface, use the DataSource property to specify the data source to bind to. When you set the DataSource property, you must manually write the code to perform the data binding. To automatically bind an HtmlSelect control to a data source represented by a data source control, use the DataSourceID property and set its value to the ID property of the data source control to use. When you set the DataSourceID property, the HtmlSelect control automatically binds to the specified data source control. Therefore, you do not need to explicitly call the DataBind method.

You can also specify which fields in the data source bind to the ListItem::Text and ListItem::Value properties of an item in the control by setting the DataTextField and DataValueField properties, respectively. If the data source contains multiple sources of data, use the DataMember property to specify the specific source to bind to the control. For example, if you have a System.Data::DataSet object with multiple tables, you must specify which table to bind to the control with the DataMember property.

For a list of initial property values for an instance of HtmlSelect, see the HtmlSelect constructor.

The following code example demonstrates how to create an HtmlSelect control by explicitly declaring the items that appear in the control.

No code example is currently available or this language may not be supported.

The following code example demonstrates how to create an HtmlSelect control by binding the control to a data source.

No code example is currently available or this language may not be supported.

The following code example demonstrates how to use the DataSourceID property to specify the data source for an HtmlSelect control. The DataSourceID property is set to the ID property of the SqlDataSource control used to retrieve the data. When the page is loaded, the HtmlSelect control automatically binds to the data source specified by the SqlDataSource control and the data is displayed to the user.

No code example is currently available or this language may not be supported.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: