ListItem.Selected プロパティ

定義

項目が選択されているかどうかを示す値を取得または設定します。

public:
 property bool Selected { bool get(); void set(bool value); };
public bool Selected { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.MinimizableAttributeTypeConverter))]
public bool Selected { get; set; }
member this.Selected : bool with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.MinimizableAttributeTypeConverter))>]
member this.Selected : bool with get, set
Public Property Selected As Boolean

プロパティ値

項目が選択されている場合は true。それ以外の場合は false。 既定値は、false です。

属性

次の例では、コントロールのコレクションCheckBoxListを反復処理するときに プロパティをSelectedItems使用して、選択されているチェック ボックスを決定する方法を示します。

注意

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

<%@ 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>ASP.NET Example</title>
</head>
 <body>
    <script language="C#" runat="server">
       void Check_Clicked(Object sender, EventArgs e) {
          Message.Text="Selected Item(s):" + "<br />" + "<br />";
          for (int i=0; i<checkboxlist1.Items.Count; i++) {
             if (checkboxlist1.Items[i].Selected)
                Message.Text=Message.Text + checkboxlist1.Items[i].Text + "<br />";
          }
       }
    </script>
 
    <form id="form1" action="checkboxlist.aspx" method="post" runat="server">
 
       <asp:CheckBoxList id="checkboxlist1" runat="server"
            AutoPostBack="True"
            CellPadding="5"
            CellSpacing="5"
            RepeatColumns="2"
            RepeatDirection="Vertical"
            RepeatLayout="Flow"
            TextAlign="Right"
            OnSelectedIndexChanged="Check_Clicked">
 
          <asp:ListItem>Item 1</asp:ListItem>
          <asp:ListItem>Item 2</asp:ListItem>
          <asp:ListItem>Item 3</asp:ListItem>
          <asp:ListItem>Item 4</asp:ListItem>
          <asp:ListItem>Item 5</asp:ListItem>
          <asp:ListItem>Item 6</asp:ListItem>
 
       </asp:CheckBoxList>
 
       <br /><br />
       <asp:label id="Message" 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>ASP.NET Example</title>
</head>
 <body>
    <script language="VB" runat="server">
       Sub Check_Clicked(sender As Object, e As EventArgs)
          Message.Text = "Selected Item(s):<br /><br />"
          Dim i As Integer
          For i = 0 To checkboxlist1.Items.Count - 1
             If checkboxlist1.Items(i).Selected Then
                Message.Text = Message.Text & checkboxlist1.Items(i).Text & "<br />"
             End If
          Next
       End Sub
    </script>
 
    <form id="form1" action="checkboxlist.aspx" method="post" runat="server">
 
       <asp:CheckBoxList id="checkboxlist1" runat="server"
            AutoPostBack="True"
            CellPadding="5"
            CellSpacing="5"
            RepeatColumns="2"
            RepeatDirection="Vertical"
            RepeatLayout="Flow"
            TextAlign="Right"
            OnSelectedIndexChanged="Check_Clicked">
 
          <asp:ListItem>Item 1</asp:ListItem>
          <asp:ListItem>Item 2</asp:ListItem>
          <asp:ListItem>Item 3</asp:ListItem>
          <asp:ListItem>Item 4</asp:ListItem>
          <asp:ListItem>Item 5</asp:ListItem>
          <asp:ListItem>Item 6</asp:ListItem>
 
       </asp:CheckBoxList>
 
       <br /><br />
       <asp:label id="Message" runat="server"/>
             
    </form>
         
 </body>
 </html>

注釈

プロパティを Selected 使用して、それを含むリスト コントロールで が現在選択されているかどうかを ListItem 確認します。 このプロパティは、項目が選択されているかどうかを判断するために、リスト コントロール内のオブジェクトの ListItem コレクションを反復処理するときに一般的に使用されます。

適用対象

こちらもご覧ください