ListItem.Enabled プロパティ

定義

リスト項目が有効であるかどうかを示す値を取得または設定します。

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

プロパティ値

リスト項目が有効である場合は true。それ以外の場合は false。 既定値は、true です。

次の例では、 プロパティを使用して、コントロール内の Enabled リスト アイテムをプログラムで無効にする方法を RadioButtonList 示します。 最初の質問では、ユーザーの職業に対応するラジオ ボタンを選択するようユーザーに求めます。 ユーザーが開発者ではないことを示した場合、2 番目のラジオ ボタン リストのリスト 項目は無効になります。 2 つ目の質問では、プライマリ プログラミング言語を選択するようユーザーに求めます。 この質問は、開発者ではないユーザーには関係ありません。

注意

次のコード サンプルでは、単一ファイル コード モデルを使用しており、分離コード ファイルに直接コピーすると正しく動作しない場合があります。 このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。 Web Forms コード モデルの詳細については、「ASP.NET Web Forms ページ コード モデル」を参照してください。

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

<!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 runat="server">
    <title>ListItem.Enabled Property Example</title>
<script runat="server">         
  
    protected void  Index_Changed(object sender, EventArgs e)
    {

        // if the user is not a developer, do not
        // ask the user to select a programming language.
        if (RadioButtonList1.SelectedIndex == 2)
        {   
            // Clear any previously selected list 
            // items in the second question.
            RadioButtonList2.SelectedIndex = -1;

            // Disable all the list items in the second question.
            for (int i = 0; i < RadioButtonList2.Items.Count; i++)
            {
                RadioButtonList2.Items[i].Enabled = false;
            }
        }          
        else
        // Enable all the list items in the second question.
            for (int i = 0; i < RadioButtonList2.Items.Count; i++)
        {
            RadioButtonList2.Items[i].Enabled = true;
        }
    }
</script>
  </head>

  <body>
    <form id="form1" runat="server">
        
      <h3>ListItem.Enabled Property Example</h3>
      
      Select your occupation:
      <asp:radiobuttonlist id="RadioButtonList1"
        autopostback="true"
        onselectedindexchanged="Index_Changed" 
    runat="server">             
      <asp:ListItem>Web developer</asp:ListItem>
      <asp:ListItem>Windows developer</asp:ListItem>
      <asp:ListItem>Occupation other than developer</asp:ListItem>
        </asp:radiobuttonlist>
          
    <br /><br />
          
    Select your primary programming language:
        <asp:radiobuttonlist id="RadioButtonList2" 
      runat="server">             
       <asp:ListItem>Visual Basic .NET</asp:ListItem>
       <asp:ListItem>C#</asp:ListItem>
       <asp:ListItem>C++</asp:ListItem>
       <asp:ListItem>Other</asp:ListItem>
    </asp:radiobuttonlist> 
                  
    </form>      
  </body>
</html>
<%@ Page Language="VB" %>

<!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 runat="server">
    <title>ListItem.Enabled Property Example</title>
<script runat="server">         
  
      Sub Index_Changed(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer

        ' If the user is not a developer, do not
        ' ask the user to select a programming language.
        If radiobuttonlist1.SelectedIndex = 2 Then
          ' Clear any previously selected list 
          ' items in the second question.
          Radiobuttonlist2.SelectedIndex = -1

          ' Disable all the list items in the second question.
          For i = 0 To Radiobuttonlist2.Items.Count - 1
            Radiobuttonlist2.Items(i).Enabled = False
          Next
          
        Else
          ' Enable all the list items in the second question.
          For i = 0 To Radiobuttonlist2.Items.Count - 1
            Radiobuttonlist2.Items(i).Enabled = True
          Next i

        End If
      End Sub
    
      </script>
  </head>

  <body>
    <form id="form1" runat="server">
        
      <h3>ListItem.Enabled Property Example</h3>
      
      Select your occupation:
      <asp:radiobuttonlist id="RadioButtonList1"
        autopostback="true"
        onselectedindexchanged="Index_Changed" 
    runat="server">             
      <asp:ListItem>Web developer</asp:ListItem>
      <asp:ListItem>Windows developer</asp:ListItem>
      <asp:ListItem>Occupation other than developer</asp:ListItem>
        </asp:radiobuttonlist>
          
    <br /><br />
          
    Select your primary programming language:
        <asp:radiobuttonlist id="RadioButtonList2" 
      runat="server">             
       <asp:ListItem>Visual Basic .NET</asp:ListItem>
       <asp:ListItem>C#</asp:ListItem>
       <asp:ListItem>C++</asp:ListItem>
       <asp:ListItem>Other</asp:ListItem>
    </asp:radiobuttonlist> 
                  
    </form>      
  </body>
</html>

注釈

Enabledプロパティを使用すると、コントロールをListItem有効または無効にするかどうかを指定できます。 ListItem無効になっているコントロールは、選択できないことを示すために淡色表示されます。 コントロールまたはコントロールのコントロールを ListItem 無効にするには、 RadioButtonList このプロパティを CheckBoxList 使用します。

注意

このプロパティを使用して、コントロールまたはListBoxコントロールのコントロールをDropDownList無効ListItemにすることはできません。

適用対象

こちらもご覧ください