Redigera

Dela via


HtmlSelect.SelectedIndex Property

Definition

Gets or sets the ordinal index of the selected item in an HtmlSelect control.

public:
 virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public virtual int SelectedIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
Public Overridable Property SelectedIndex As Integer

Property Value

The ordinal index of the selected item in an HtmlSelect control. A value of -1 indicates that no item is selected.

Attributes

Exceptions

The property was set to a value greater than the number of items in the HtmlSelect control or less than -1.

Examples

The following code example demonstrates how to use the SelectedIndex property to determine the index of the selected item in the HtmlSelect control. The index is then used to retrieve the selected item from the Items collection.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
<script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {
        
         Label1.Text = "You selected the item with index number " + 
                       Select1.SelectedIndex.ToString() + 
                       " and contains the value " +
                       Select1.Value + ".";

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

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

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

         <option value="Text for Item 1" selected="selected"> Item 1 </option>
         <option value="Text for Item 2"> Item 2 </option>
         <option value="Text for Item 3"> Item 3 </option>
         <option value="Text for Item 4"> Item 4 </option>
         <option value="Text for Item 5"> Item 5 </option>
         <option value="Text for Item 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>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
<script runat="server">

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Label1.Text = "You selected the item with index number " & _
                       Select1.SelectedIndex.ToString() & _
                       " and contains the value " & _
                       Select1.Value & "."

      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

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

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

         <option value="Text for Item 1" selected="selected"> Item 1 </option>
         <option value="Text for Item 2"> Item 2 </option>
         <option value="Text for Item 3"> Item 3 </option>
         <option value="Text for Item 4"> Item 4 </option>
         <option value="Text for Item 5"> Item 5 </option>
         <option value="Text for Item 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>

Remarks

The SelectedIndex property is commonly used to determine the index of the selected item in a single-selection HtmlSelect control. This index can then be used to retrieve the item from the Items collection.

If the Multiple property is set to true, indicating that multiple items can be concurrently selected, the SelectedIndex property contains the index of the first selected item. To determine the selected items from an HtmlSelect control that allows multiple simultaneous selections, iterate through the Items collection and test the ListItem.Selected property of each item.

Caution

It is possible to have no item selected. If no item is selected, the SelectedIndex property contains a value of -1. This commonly occurs when the page first loads and an item is not selected by default. Provide code to test this value before referencing the item in the Items collection. Otherwise, an exception is thrown if the index is out of the range of the collection.

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. If a drop-down list box is displayed, an item is always selected. If a list box is displayed, you can programmatically clear the selection from all items by setting the SelectedIndex property to -1.

Applies to

See also