This documentation is archived and is not being maintained.

HtmlSelect.Items Property

Gets a collection that contains the items listed in an HtmlSelect control.

[Visual Basic]
Public ReadOnly Property Items As ListItemCollection
[C#]
public ListItemCollection Items {get;}
[C++]
public: __property ListItemCollection* get_Items();
[JScript]
public function get Items() : ListItemCollection;

Property Value

A System.Web.UI.WebControls.ListItemCollection that contains the items listed in an HtmlSelect control.

Remarks

Use the Items collection to manage the items listed in the HtmlSelect control. You can programmatically add items to, remove item from, and insert items into the collection.

The Items collection is commonly used to iterate through the items in the HtmlSelect control. For example, when multiple items are selected, you can iterate through the Items collection to determine which items are selected.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the Items collection to iterate through the items in the HtmlSelect control and determine which items are selected.

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

[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

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

See Also

HtmlSelect Class | HtmlSelect Members | System.Web.UI.HtmlControls Namespace | System.Web.UI.WebControls.ListItemCollection

Show: