ListBox 类

表示允许单项或多项选择的列表框控件。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<ValidationPropertyAttribute("SelectedItem")> _
Public Class ListBox
    Inherits ListControl
    Implements IPostBackDataHandler
用法
Dim instance As ListBox
[ValidationPropertyAttribute("SelectedItem")] 
public class ListBox : ListControl, IPostBackDataHandler
[ValidationPropertyAttribute(L"SelectedItem")] 
public ref class ListBox : public ListControl, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */ 
public class ListBox extends ListControl implements IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") 
public class ListBox extends ListControl implements IPostBackDataHandler

备注

使用 ListBox 控件创建允许单项或多项选择的列表控件。使用 Rows 属性指定控件的高度。若要启用多项选择,请将 SelectionMode 属性设置为 ListSelectionMode.Multiple

可以使用 Items 集合检查 ListBox 控件中包含的 ListItem 对象。例如,可以通过枚举 Items 集合并测试每个 ListItem 元素的 Selected 值来确定 ListBox 控件中的选定项。

警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

辅助功能

默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:使用 ASP.NET 用户控件创建可重用元素 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 ListBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:使用 ASP.NET 用户控件创建可重用元素 使用 Visual Web Developer 生成应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 使用 Visual Web Developer 生成应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 ListBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 ListBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 ListBox Web 服务器控件 生成 ASP .NET Web 应用程序
如何:从数据源填充 List Web 服务器控件 生成 ASP .NET Web 应用程序
如何:在 List Web 服务器控件中添加项 生成 ASP .NET Web 应用程序
如何:设置 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序

示例

下面的示例说明如何创建 ListBox 控件。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <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 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-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
        
   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <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 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-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>
        
   </form>

</body>
</html>

下面的示例说明如何通过数据绑定创建 ListBox 控件。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <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 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-name="Verdana" 
             font-size="10pt" 
             runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <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 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-name="Verdana" 
             font-size="10pt" 
             runat="server"/>

   </form>

</body>
</html>

.NET Framework 安全性

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListBox 成员
System.Web.UI.WebControls 命名空间
ListControl
ListItem
ListItemCollection

其他资源

ListBox Web 服务器控件
保证标准控件的安全
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入