更新:2007 年 11 月
命名空间:
System.Web.UI.WebControls 程序集:
System.Web(在 System.Web.dll 中)
<ValidationPropertyAttribute("SelectedItem")> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class ListBox _
Inherits ListControl _
Implements IPostBackDataHandler
[ValidationPropertyAttribute("SelectedItem")]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ListBox : ListControl, IPostBackDataHandler
[ValidationPropertyAttribute(L"SelectedItem")]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class ListBox : public ListControl,
IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class ListBox extends ListControl implements IPostBackDataHandler
public class ListBox extends ListControl implements IPostBackDataHandler
使用 ListBox 控件创建允许单项或多项选择的列表控件。使用 Rows 属性指定控件的高度。若要启用多项选择,请将 SelectionMode 属性设置为 ListSelectionMode.Multiple。
可以使用 Items 集合检查 ListBox 控件中包含的 ListItem 对象。例如,可以通过枚举 Items 集合并测试每个 ListItem 元素的 Selected 值来确定 ListBox 控件中的选定项。
辅助功能
默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能。
下面的示例说明如何创建 ListBox 控件。
<%@ 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 >
<head>
<title>ListBox Example</title>
<script language="VB" runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > - 1 Then
Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
End If
End Sub 'SubmitBtn_Click
</script>
</head>
<body>
<h3>ListBox Example</h3>
<form id="form1" runat="server">
<asp:ListBox id="ListBox1"
Rows="6"
Width="100px"
SelectionMode="Single"
runat="server">
<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:ListBox>
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<asp:Label id="Label1"
Font-Names="Verdana"
Font-Size="10pt"
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 >
<head>
<title>ListBox Example</title>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e)
{
if (ListBox1.SelectedIndex > -1)
Label1.Text="You chose: " + ListBox1.SelectedItem.Text;
}
</script>
</head>
<body>
<h3>ListBox Example</h3>
<form id="form1" runat="server">
<asp:ListBox id="ListBox1"
Rows="6"
Width="100px"
SelectionMode="Single"
runat="server">
<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:ListBox>
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<asp:Label id="Label1"
Font-Names="Verdana"
Font-Size="10pt"
runat="server"/>
</form>
</body>
</html>
下面的示例说明如何通过数据绑定创建 ListBox 控件。
<%@ 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 >
<head>
<title>Data Binding ListBox</title>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values As New ArrayList()
values.Add("Item 1")
values.Add("Item 2")
values.Add("Item 3")
values.Add("Item 4")
values.Add("Item 5")
values.Add("Item 6")
ListBox1.DataSource = values
ListBox1.DataBind()
End If
End Sub 'Page_Load
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
If ListBox1.SelectedIndex > - 1 Then
Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
End If
End Sub 'SubmitBtn_Click
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Data Binding ListBox</h3>
<asp:ListBox id="ListBox1"
Width="100px"
runat="server"/>
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<asp:Label id="Label1"
Font-Names="Verdana"
font-size="10pt"
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 >
<head>
<title>Data Binding ListBox</title>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();
values.Add ("Item 1");
values.Add ("Item 2");
values.Add ("Item 3");
values.Add ("Item 4");
values.Add ("Item 5");
values.Add ("Item 6");
ListBox1.DataSource = values;
ListBox1.DataBind();
}
}
void SubmitBtn_Click(Object sender, EventArgs e)
{
if ( ListBox1.SelectedIndex > -1 )
Label1.Text = "You chose: " + ListBox1.SelectedItem.Text;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Data Binding ListBox</h3>
<asp:ListBox id="ListBox1"
Width="100px"
runat="server"/>
<asp:button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server" />
<asp:Label id="Label1"
Font-Names="Verdana"
font-size="10pt"
runat="server"/>
</form>
</body>
</html>
System..::.Object
System.Web.UI..::.Control
System.Web.UI.WebControls..::.WebControl
System.Web.UI.WebControls..::.BaseDataBoundControl
System.Web.UI.WebControls..::.DataBoundControl
System.Web.UI.WebControls..::.ListControl
System.Web.UI.WebControls..::.ListBox
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
参考
其他资源