ListItem.Selected プロパティ
項目が選択されているかどうかを示す値を取得または設定します。
名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文
'宣言
<TypeConverterAttribute(GetType(MinimizableAttributeTypeConverter))> _
Public Property Selected As Boolean
'使用
Dim instance As ListItem
Dim value As Boolean
value = instance.Selected
instance.Selected = value
[TypeConverterAttribute(typeof(MinimizableAttributeTypeConverter))]
public bool Selected { get; set; }
[TypeConverterAttribute(typeof(MinimizableAttributeTypeConverter))]
public:
property bool Selected {
bool get ();
void set (bool value);
}
/** @property */
public boolean get_Selected ()
/** @property */
public void set_Selected (boolean value)
public function get Selected () : boolean
public function set Selected (value : boolean)
適用できません。
プロパティ値
項目が選択されている場合は true。それ以外の場合は false。既定値は false です。
解説
Selected プロパティを使用して、ListItem がそれを格納しているリスト コントロールで現在選択されているかどうかを確認します。通常、このプロパティは、リスト コントロール内の ListItem オブジェクトのコレクションを反復処理するときに、項目が選択されているかどうかを確認するために使用します。
トピック | 場所 |
---|---|
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) | Visual Studio での ASP .NET Web アプリケーションの作成 |
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) | Visual Studio ASP .NET での Web アプリケーションの作成 |
方法 : リスト Web サーバー コントロールの選択項目を設定する (Visual Studio) | Visual Studio ASP .NET での Web アプリケーションの作成 |
方法 : リスト Web サーバー コントロールの選択項目を設定する | ASP .NET Web アプリケーションの作成 |
使用例
CheckBoxList コントロールの Items コレクションを反復処理するときに、Selected プロパティを使用して選択されているチェック ボックスを確認する方法の例を次に示します。
メモ : |
---|
次のコード サンプルはシングルファイル コード モデルを使用しており、分離コード ファイルに直接コピーされた場合は正常に動作しない可能性があります。このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。Web フォームのコード モデルの詳細については、「ASP.NET Web ページのコード モデル」を参照してください。 |
<%@ 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>
<%@ 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="JScript" 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="JSCRIPT" runat="server">
function Check_Clicked(sender : Object, e : EventArgs){
Message.Text = "Selected Item(s):<br /><br />"
for(var 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>
プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
参照
関連項目
ListItem クラス
ListItem メンバ
System.Web.UI.WebControls 名前空間
ListControl.Items プロパティ
CheckBoxList
ListControl
RadioButtonList
ListBox
DropDownList
その他の技術情報
ListBox Web サーバー コントロール
RadioButton Web サーバー コントロールおよび RadioButtonList Web サーバー コントロールの概要
CheckBox Web サーバー コントロールおよび CheckBoxList Web サーバー コントロール
BulletedList Web サーバー コントロール
DropDownList Web サーバー コントロール