Represents a list box control that allows single or multiple item selection.
<ValidationPropertyAttribute("SelectedItem")> _ <AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class ListBox _ Inherits ListControl _ Implements IPostBackDataHandler
Dim instance As ListBox
[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
public class ListBox extends ListControl implements IPostBackDataHandler
<asp:ListBox />
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.
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 Controls, How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.
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.
The following example demonstrates how to create a ListBox control.
<%@ 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>
The following example shows how to create a ListBox control that supports multiple selections.
The following example demonstrates how to create a ListBox control through data binding.
<%@ 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>
for operating in a hosted environment. Demand value: LinkDemand. Permission value: Minimal.
for operating in a hosted environment. Demand value: InheritanceDemand. Permission value: Minimal.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98