Web Control: The List Box
A list box is a control that presents a vertical list of items with each item on its own line. A list box allows a web page visitor to select one or more…
ASP.NET Web Controls: The List Box
Web Control: The List Box#
List Box Fundamentals#
Introduction#
The ListBox control displays a vertical column of choices, where every option occupies its own row. It enables visitors to pick one or several entries from the collection. In the context of an ASP.NET application, this control is an instance of the ListBox class, which belongs to the System.Web.UI.WebControls namespace in the System.Web.dll assembly.
Creating a List Box#
To visually create a list box, from the Standard section of the Toolbox, you can drag the ListBox object and drop it on the form. To create its list of items, on the form, click the list box. Then:
- You can click its arrow button and click Edit Items. This would display the ListItem Collection Editor:
- In the Properties window, click Items and click browse button
To manually create a list box, create an <asp:ListBox> tag on the form:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h3>Exercise</h3>
<form id="frmRealEstate" method="post" runat="server">
<asp:ListBox id="TypesOfHouses" runat="server"></asp:ListBox>
</form>
</body>
</html>This would produce:
Characteristics of a List Box#
The Items of a List Box#
As mentioned already, a list box contains one or more items. Each is represented with text. To add an item to the list:
- If you are using the ListItem Collection Editor, click Add
- If you are working manually, between the opening and the closing tags, create an asp:ListItem tag with its own starting and closing tags:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:RadioButtonList id="Genders" runat="server">
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>To specify the text of an item, between its beginning and its ending tags, type the desired string. Here are examples:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:ListBox id="TypesOfHouses" runat="server">
<asp:ListItem>Condominium</asp:ListItem>
<asp:ListItem>Townhouse</asp:ListItem>
<asp:ListItem>Single Family</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>This would produce:
The Visible Rows of a List Box#
Naturally, the purpose of a ListBox is to show its contained items, with a default view of 4 rows. You can adjust how many items are shown simultaneously by setting the Rows attribute to your preferred number. For instance:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:ListBox id="TypesOfHouses" rows="4" runat="server">
<asp:ListItem>Condominium</asp:ListItem>
<asp:ListItem>Townhouse</asp:ListItem>
<asp:ListItem>Single Family</asp:ListItem>
<asp:ListItem>Trailer</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>This would produce:
If the list contains less than 4 items, there would be an empty area under the last item:
If there are are more than 4 items, the list box would display a scroll bar on its right side. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:ListBox id="TypesOfHouses" rows="4" runat="server">
<asp:ListItem>Condo Studio</asp:ListItem>
<asp:ListItem>Condo Apartment</asp:ListItem>
<asp:ListItem>Townhouse</asp:ListItem>
<asp:ListItem>Single Family</asp:ListItem>
<asp:ListItem>Trailer</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
</asp:ListBox>
</form>
</body>
</html>The Selection Mode of a List Box#
If a list box contains more than one item, you can control whether the user is allowed to select only one or more than one item at a time. To assist you with this, the <asp:ListBox> tag is equipped with an attribute named SelectionMode. Its two values are Single and Multiple and they are members of the ListSelectionMode enumeration. By default, a list box allows the user to select only one item because the default value of the SelectionMode attribute is Single. If you want to allow the user to bee able to select many items, assign the Multiple string to this attribute.
Making a Selection on a List Box#
Selecting an entry in a ListBox is typically done by clicking it, which causes the item to be highlighted. Depending on the configuration, users might be able to select multiple items at once. There are two primary ways to respond to these selections in code. The SelectedIndexChanged event is triggered when the index of the chosen item changes. Alternatively, the TextChanged event occurs when the control updates its content based on the selection. Developers can use these events to execute logic once the user's choice is transmitted to the server. Home Copyright © 2009 C# Key

