asp.net.ph

Skip Navigation Links

ListBox Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


Creates a single-selection or multi-selection list box.

Declarative Syntax

For information on the individual members of this class, see ListBox in the class library.

Remarks

The ListBox control renders a list control that allows users to select single or multiple items. Use the Rows property to specify the height of the control. To enable multiple item selection, set the ListBox.SelectionMode Property property to ListSelectionMode.Multiple.

Syntax Example

The below code snippet demonstrates how to use a simple ListBox control.

Within the <head> of a Web Forms page:

<script language="C#" runat="server">

   void SubmitBtn_Click ( Object sender, EventArgs e ) {
      if ( ListBox1.SelectedIndex > -1 ) {
         msgLabel.Text = "The first item you chose: " + ListBox1.SelectedItem.Text;
      }
   }

</script>
  C# VB

Within the <body> of a Web Forms page:

<form runat=server>
   <asp:ListBox id="ListBox1" Rows = "6"
         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" />

   <p>
   <asp:Label id="msgLabel" runat="server" />

</form>
See Also

ListBox Class   ListBox Web Server Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note