ASP.NET - List Control - ListBox

The ListBox control is a server control in ASP.NET that displays a list of items that the user can select one or more items from. The items can be populated statically at design time or dynamically at run time by binding the control to a data source.

Events:

  • SelectedIndexChanged: This event is raised when the selected index of the ListBox changes. You can handle this event to perform some action when the user selects a new item in the list.

Methods:

  • ClearSelection: This method clears the selection of all items in the ListBox control.
  • FindByText: This method searches for an item in the ListBox by its text value and returns its index.
  • FindByValue: This method searches for an item in the ListBox by its value and returns its index.
  • Items.Add: This method adds a new item to the ListBox control.

Properties:

  • Items: This property gets the collection of items in the ListBox control.
  • SelectionMode: This property specifies whether the ListBox control allows single or multiple items to be selected.
  • SelectedIndex: This property gets or sets the index of the currently selected item in the ListBox control.
  • SelectedItem: This property gets the currently selected item in the ListBox control.

Example

Let's say we want to create a ListBox control that displays a list of colors and allows the user to select one or more of them. Here's the code we would use:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="www.w3.org;
<head runat="server">
    <title>ListBox Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="lstColors" runat="server" SelectionMode="Multiple">
                <asp:ListItem Text="Red" Value="1" />
                <asp:ListItem Text="Green" Value="2" />
                <asp:ListItem Text="Blue" Value="3" />
                <asp:ListItem Text="Yellow" Value="4" />
            </asp:ListBox>
            <br />
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

In this example, we've included a ListBox control with an ID of "lstColors" and set its SelectionMode property to "Multiple" to allow multiple items to be selected. We've also added four items to the ListBox using the asp:ListItem element.

We've also included a button with an ID of "btnSubmit" that the user can click to submit their selection. When the button is clicked, the code in the btnSubmit_Click event handler will be executed.

Here's the code for the event handler in the code-behind file (Default.aspx.cs):

protected void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (ListItem item in lstColors.Items)
    {
        if (item.Selected)
        {
            Response.Write("You selected " + item.Text + "");
        }
    }
}

This code loops through each item in the ListBox control and checks if it has been selected by the user. If an item is selected, it writes a message to the response that displays the text of the selected item.

That's a complete example of how to use the ListBox control in ASP.NET, including the HTML markup and server-side code. You can copy and paste this code into a new ASP.NET project and run it to see the ListBox control in action.