HtmlSelect Class

HtmlSelect Class

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

For a list of all members of this type, see HtmlSelect Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.HtmlControls.HtmlControl
         System.Web.UI.HtmlControls.HtmlContainerControl
            System.Web.UI.HtmlControls.HtmlSelect

[Visual Basic]
Public Class HtmlSelect
   Inherits HtmlContainerControl
   Implements IPostBackDataHandler
[C#]
public class HtmlSelect : HtmlContainerControl,
   IPostBackDataHandler
[C++]
public __gc class HtmlSelect : public HtmlContainerControl,
   IPostBackDataHandler
[JScript]
public class HtmlSelect extends HtmlContainerControl implements
   IPostBackDataHandler

Thread Safety

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

Remarks

Use the HtmlSelect control to create a selection box. Specify items 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. 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 control allows you to bind the control to a data source. Use the DataSource property to specify the data source to bind to. You can also specify which field in the data source binds 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 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.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to create an HtmlSelect control by explicitly declaring the items that appear in the control.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Dim i As Integer

         Label1.Text = "You selected:"

         For i = 0 to Select1.Items.Count - 1
  
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br> &nbsp;&nbsp; -" & Select1.Items(i).Text
            End If         

         Next i

      End Sub

   </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>


[C#] 

<%@ 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>


[JScript] 

<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>

<head>

   <script runat="server">

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

         for (var i : int = 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>

[Visual Basic, C#, JScript] The following example demonstrates how to create an HtmlSelect control by binding the control to a data source.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      Sub Page_Load (sender As Object, e As EventArgs)
  
        ' Bind the HtmlSelect control to a data source when the page is initially loaded.
        If Not IsPostBack Then
        
           ' Open a connection to the database and run the query.
           ' Note that the connection string may vary depending on your
           ' database server settings.
           Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI"
           Dim QueryString As String = "select * from authors"

           Dim myConnection As SqlConnection = New SqlConnection(ConnectString)
           Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)

           ' Create a dataset to store the query results.
           Dim ds As DataSet = 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()
        
        End If

      End Sub

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Dim i As Integer

         Label1.Text = "You selected:"

         For i = 0 To Select1.Items.Count - 1
         
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br> &nbsp;&nbsp; - " & Select1.Items(i).Text
            End If

         Next i

      End Sub

   </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>

[C#] 
<%@ 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>

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

   <script runat="server">

      function Page_Load (sender : Object, e : EventArgs) : void
      {

        // 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. 
           var ConnectString : String = "server=localhost;database=pubs;integrated security=SSPI";
           var QueryString : String = "select * from authors";

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

           // Create a dataset to store the query results.
           var ds : DataSet  = 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();
        }

      }

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

         for (var i: int=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>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.HtmlControls

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

HtmlSelect Members | System.Web.UI.HtmlControls Namespace | Size | Multiple | SelectedIndex | Items | System.Web.UI.WebControls.ListItem | System.Web.UI.WebControls.ListItemCollection | DataSource | DataMember | System.Data.DataSet | DataTextField | ListItem.Text | DataValueField | ListItem.Value | InnerHtml | InnerText

Show:
© 2016 Microsoft