共用方式為


DropDownList.SelectedIndex 屬性

定義

取得或設定 DropDownList 控制項中選取之項目的索引。

public:
 virtual property int SelectedIndex { int get(); void set(int value); };
public override int SelectedIndex { get; set; }
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer

屬性值

Int32

DropDownList 控制項中選取之項目的索引。 預設值為 0,將選取清單中的第一個項目。

範例

下列程式碼範例示範如何使用 SelectedIndex 屬性來判斷 控制項中的 DropDownList 選取專案。

<%@ 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" >
   <script runat="server" >
  
      void Selection_Change(Object sender, EventArgs e)
      {

         // Set the title format based on the value selected from the
         // DropDownList control. The SelectedIndex property can be used
         // to determine an enumeration value. Simply cast the value to 
         // the appropriate enumeration type. Note that the value might
         // need to be adjusted if the enumeration type is not zero-based. 
         Calendar1.TitleFormat = (TitleFormat)ModeList.SelectedIndex;

      }
  
   </script>
  
<head runat="server">
    <title> DropDownList SelectedIndex Example </title>
</head>
<body>

   <form id="form1" runat="server">
  
      <h3> DropDownList SelectedIndex Example </h3>

      Choose the title format.

      <br /><br /> 
  
      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Format:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ModeList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem> Month </asp:ListItem>
                  <asp:ListItem Selected="True"> MonthYear </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>
         
      </table>   
  
   </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" >
   <script runat="server" >
  
      Sub Selection_Change(sender As Object, e As EventArgs)
      
         ' Set the title format based on the value selected from the
         ' DropDownList control. The SelectedIndex property can be used
         ' to determine an enumeration value. Simply cast the value to 
         ' the appropriate enumeration type. Note that the value might
         ' need to be adjusted if the enumeration type is not zero-based.
         Calendar1.TitleFormat = CType(ModeList.SelectedIndex, TitleFormat)

      End Sub
  
   </script>
  
<head runat="server">
    <title> DropDownList SelectedIndex Example </title>
</head>
<body>

   <form id="form1" runat="server">
  
      <h3> DropDownList SelectedIndex Example </h3>

      Choose the title format.

      <br /><br /> 
  
      <asp:Calendar id="Calendar1"
           ShowGridLines="True" 
           ShowTitle="True"
           runat="server"/>

      <br /><br />

      <table cellpadding="5">

         <tr>

            <td>

               Format:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ModeList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem> Month </asp:ListItem>
                  <asp:ListItem Selected="True"> MonthYear </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>
         
      </table>   
  
   </form>

</body>
</html>

備註

SelectedIndex使用 屬性,以程式設計方式指定或決定控制項 DropDownList 中選取專案的索引。 控制項中 DropDownList 一律會選取專案。 您無法同時清除清單中每個專案的選取範圍。

注意

控制項中 DropDownList 專案的索引是以零起始。

適用於

另請參閱