Export (0) Print
Expand All

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("Value")] 
public class HtmlSelect : HtmlContainerControl, IPostBackDataHandler, IParserAccessor
/** @attribute ValidationPropertyAttribute("Value") */ 
public class HtmlSelect extends HtmlContainerControl implements IPostBackDataHandler, IParserAccessor
ValidationPropertyAttribute("Value") 
public class HtmlSelect extends HtmlContainerControl implements IPostBackDataHandler, IParserAccessor

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.


<%@ Page Language="C#" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {
        
         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; -" + Select1.Items[i].Text;
         }

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br><br>

      <select id="Select1" 
              Multiple="True"
              runat="server">

         <option value="1" Selected="True"> Item 1 </option>
         <option value="2"> Item 2 </option>
         <option value="3"> Item 3 </option>
         <option value="4"> Item 4 </option>
         <option value="5"> Item 5 </option>
         <option value="6"> Item 6 </option>

      </select>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>


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

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      void Page_Load (Object sender, EventArgs e)
      {

        // Bind the HtmlSelect control to a data source when the page is initially loaded.
        if (!IsPostBack)
        {
      
           // Open a connection to the database and run the query.
           // Note that the connection string may vary depending on your
           // database server settings. 
           string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
           string QueryString = "select * from authors";

           SqlConnection myConnection = new SqlConnection(ConnectString);
           SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);

           // Create a dataset to store the query results.
           DataSet ds = new DataSet();
           myCommand.Fill(ds, "Authors");

           // Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds;
           Select1.DataTextField = "au_fname";
           Select1.DataValueField = "au_fname";
           Select1.DataBind();
        }

      }

      void Button_Click (Object sender, EventArgs e)
      {
       
         // Display the selected items. 
         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
            if (Select1.Items[i].Selected)
               Label1.Text += "<br> &nbsp;&nbsp; - " + Select1.Items[i].Text;
         }

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br>
      Use the Control or Shift key to select multiple items. <br><br>

      <select id="Select1"
              Multiple="True" 
              runat="server"/>

      <br><br>

      <button id="Button1"
              OnServerClick="Button_Click"
              runat="server">

         Submit

      </button>

      <br><br>

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>

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.

<%@ page language="C#" %>

<html>
<head id="Head1" runat="server">
    <title>HtmlSelect.DataSourceID</title>
</head>
<body>
  <form id="Form1" runat="server">  

    <h3> HtmlSelect.DataSourceID Example </h3>

    <p>Select an item from the list</p>

    <select id="Select1"
      name="Select1"
      DataSourceId="SqlDataSource1"
      datatextfield="ProductName"
      runat="server">
    </select>
       
    <asp:sqldatasource id="SqlDataSource1" 		 
		  connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>

   </form>

</body>

</html>

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 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft