Share via


HtmlSelect.SelectedIndex 屬性

定義

取得或設定 HtmlSelect 控制項中所選取項目的循序的索引。

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

屬性值

HtmlSelect 控制項中所選取項目的循序索引。 值為 -1,代表並沒有選取的項目。

屬性

例外狀況

屬性設定為大於 HtmlSelect 控制項中項目數的值,或小於 -1 的值。

範例

下列程式碼範例示範如何使用 SelectedIndex 屬性來判斷控制項中 HtmlSelect 所選項目的索引。 然後,索引會用來從 Items 集合中擷取選取的專案。


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

備註

屬性 SelectedIndex 通常用來判斷單一選取 HtmlSelect 控制項中所選項目的索引。 然後,這個索引可用來從集合中 Items 擷取專案。

Multiple如果 屬性設定 true 為 ,表示可以同時選取多個專案,則 SelectedIndex 屬性會包含第一個選取專案的索引。 若要從允許多個同時選取專案的控制項中判斷選取的專案,請逐一 HtmlSelect 查看 Items 集合,並測試 ListItem.Selected 每個專案的 屬性。

警告

可能未選取任何專案。 如果未選取任何專案,則 SelectedIndex 屬性會包含 的值 -1 。 這通常發生在頁面第一次載入且預設未選取專案時。 提供程式碼以在參考集合中的 Items 專案之前測試此值。 否則,如果索引超出集合的範圍,則會擲回例外狀況。

根據預設, HtmlSelect 控制項會顯示為下拉式清單方塊。 如果您允許多個選取 (專案,方法是將 屬性設定 Multipletrue) ,或藉由將 Size 屬性設定為大於) 的值 1 ,指定大於 (一列的高度,控制項會顯示為清單方塊。 如果顯示下拉式清單方塊,一律會選取專案。 如果顯示清單方塊,您可以將 屬性設定 SelectedIndex-1 ,以程式設計方式清除所有專案的選取範圍。

適用於

另請參閱