ListBox Class
Represents a list box control that allows single or multiple item selection.
For a list of all members of this type, see ListBox Members.
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.ListControl
System.Web.UI.WebControls.ListBox
[Visual Basic] Public Class ListBox Inherits ListControl Implements IPostBackDataHandler [C#] public class ListBox : ListControl, IPostBackDataHandler [C++] public __gc class ListBox : public ListControl, IPostBackDataHandler [JScript] public class ListBox extends ListControl implements IPostBackDataHandler
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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.
Example
[Visual Basic, C#] The following example demonstrates how to create a ListBox control.
[Visual Basic] <%@ 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> [C#] <%@ 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>
[Visual Basic, C#] The following example demonstrates how to create a ListBox control through data binding.
[Visual Basic] <%@ 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> [C#] <%@ 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>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.WebControls
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
ListBox Members | System.Web.UI.WebControls Namespace | ListControl | ListItem | ListItemCollection