ListControl.Items 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取列表控件项的集合。
public:
virtual property System::Web::UI::WebControls::ListItemCollection ^ Items { System::Web::UI::WebControls::ListItemCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)]
public virtual System.Web.UI.WebControls.ListItemCollection Items { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)>]
member this.Items : System.Web.UI.WebControls.ListItemCollection
Public Overridable ReadOnly Property Items As ListItemCollection
属性值
列表中的项。 默认为空列表。
- 属性
示例
下面的示例演示如何使用 Items 属性来确定控件中的 CheckBoxList 选定项。
<%@ 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 runat="server">
<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" 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 runat="server">
<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" 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>
注解
使用此属性获取列表控件中项的属性。 此属性可用于确定列表控件中的选定项。