Export (0) Print
Expand All

ListBox Class

Represents a list box control that allows single or multiple item selection.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

[ValidationPropertyAttribute("SelectedItem")] 
public class ListBox : ListControl, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */ 
public class ListBox extends ListControl implements IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") 
public class ListBox extends ListControl implements IPostBackDataHandler

Use the ListBox control to create a list control that allows single or multiple item selection. Use the Rows property to specify the height of the control. To enable multiple item selection, set the SelectionMode property to ListSelectionMode.Multiple.

Use the Items collection to examine the ListItem objects contained in the ListBox control. For example, you can determine the selected item(s) in the ListBox control by enumerating the Items collection and testing the Selected value for each ListItem element.

Caution noteCaution

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a control. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Securing Standard ControlsHow to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Respond to Changes in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Determine the Selection in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Add ListBox Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Populate List Web Server Controls from a Data SourceBuilding ASP .NET Web Applications
How to: Add Items in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Set the Selection in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Respond to Changes in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Determine the Selection in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Add ListBox Web Server Controls to a Web Forms PageBuilding ASP .NET Web Applications
How to: Populate List Web Server Controls from a Data SourceBuilding ASP .NET Web Applications
How to: Add Items in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Set the Selection in List Web Server ControlsBuilding ASP .NET Web Applications
How to: Set the Selection in List Web Server Controls (Visual Studio)Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating Reusable Elements with ASP.NET User ControlsBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
How to: Add ListBox Web Server Controls to a Web Forms Page (Visual Studio)Building ASP .NET Web Applications in Visual Studio
How to: Populate List Web Server Controls from a Data Source (Visual Studio)Building ASP .NET Web Applications in Visual Studio
Walkthrough: Data Binding to a Custom Business ObjectBuilding ASP .NET Web Applications in Visual Studio
How to: Add Items in List Web Server Controls (Visual Studio)Building ASP .NET Web Applications in Visual Studio

The following example demonstrates how to create a ListBox control.

<%@ 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>

The following example demonstrates how to create a ListBox control through data binding.

<%@ 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>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft