System.Web.UI.HtmlControls.HtmlSelect Class

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

See Also: HtmlSelect Members


public class HtmlSelect : HtmlContainerControl, System.Web.UI.IPostBackDataHandler


Use the System.Web.UI.HtmlControls.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 System.Web.UI.WebControls.ListItem.Text property of the item, or simply place text between the opening and closing <option> tags. By setting the System.Web.UI.WebControls.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 System.Web.UI.WebControls.ListItem.Selected property of the item to true.

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

By default, the System.Web.UI.HtmlControls.HtmlSelect control is displayed as a drop-down list box. If you allow multiple selections (by setting the HtmlSelect.Multiple property to true) or specify a height greater than one row (by setting the HtmlSelect.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 System.Web.UI.HtmlControls.HtmlSelect control, use the HtmlSelect.SelectedIndex property to get the index of the selected item. You can then use this value to retrieve the item from the HtmlSelect.Items collection.

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

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

You can also specify which fields in the data source bind to the System.Web.UI.WebControls.ListItem.Text and System.Web.UI.WebControls.ListItem.Value properties of an item in the control by setting the HtmlSelect.DataTextField and HtmlSelect.DataValueField properties, respectively. If the data source contains multiple sources of data, use the HtmlSelect.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 HtmlSelect.DataMember property.

For a list of initial property values for an instance of System.Web.UI.HtmlControls.HtmlSelect, see the HtmlSelect.#ctor constructor.


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