ListItem.Selected 属性
定义
获取或设置指示是否选定了项的值。Gets or sets a value indicating whether the item is 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。true if the item is selected; otherwise, false. 默认值为 false。The default is false.
- 属性
示例
下面的示例演示如何 Selected 在循环访问控件的集合时使用属性 Items CheckBoxList 来确定选中了哪些复选框。The following example demonstrates how to use the Selected property when iterating through the Items collection of a CheckBoxList control to determine which check boxes are selected.
备注
下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,则可能无法正常工作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。This code sample must be copied into an empty text file that has an .aspx extension. 有关 Web 窗体代码模型的详细信息,请参阅 ASP.NET Web 窗体页代码模型。For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.
<%@ 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 当前在包含它的列表控件中是否处于选定状态。Use the Selected property to determine whether the ListItem is currently selected in the list control that contains it. 当遍历列表控件中的对象集合时,通常使用此属性 ListItem 来确定是否选择了某个项。This property is commonly used, when iterating through a collection of ListItem objects in a list control, to determine whether an item is selected.
适用于
另请参阅
- Items
- CheckBoxList
- ListControl
- RadioButtonList
- ListBox
- DropDownList
- ListBox Web 服务器控件概述ListBox Web Server Control Overview
- 单选按钮和 RadioButtonList Web 服务器控件概述RadioButton and RadioButtonList Web Server Controls Overview
- CheckBox 和 CheckBoxList Web 服务器控件概述CheckBox and CheckBoxList Web Server Controls Overview
- BulletedList Web 服务器控件概述BulletedList Web Server Control Overview
- DropDownList Web 服务器控件概述DropDownList Web Server Control Overview