Home > DeveloperSection > Beginner > ListBox in ASP.Net

ListBox in ASP.Net


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 3484  View(s)
Rate this:

ListBox Control in ASP.Net

The List Box server control displays a list of items. We can select one or more items from the list of items displayed. The List Box control is used to create a single- or multi-selection drop-down list.

In single selection ListBox we can select only single value at a time.

In a multiple selection List Box we can select multiple values at a time.

We can change selection mode by ListBox SelectionMode Property

ListBox Control in ASP.Net

ListBox Event

SelectedIndexChanged Event: This event is fired when the selection of ListBox changes.

Single Selection ListBox

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Font-Bold="True"Font-Italic="True" ForeColor="#FF0066"

        onselectedindexchanged="ListBox1_SelectedIndexChanged">

        <asp:ListItem>Select</asp:ListItem>

        <asp:ListItem>1</asp:ListItem>

        <asp:ListItem>2</asp:ListItem>

        <asp:ListItem>3</asp:ListItem>

        <asp:ListItem>4</asp:ListItem>

        <asp:ListItem></asp:ListItem>

        <asp:ListItem>5</asp:ListItem>

        <asp:ListItem></asp:ListItem>

 </asp:ListBox>

<asp:Label ID="Label1" runat="server"></asp:Label>

 

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Label1.Text = ListBox1.SelectedValue;

    }

 

ListBox Control in ASP.Net

Multiple Selection ListBox 

To change the selection of ListBox from single selection to Multiple Selection you can set its SelectionMode property to Multiple.

        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">

            <asp:ListItem>Select</asp:ListItem>

            <asp:ListItem>1</asp:ListItem>

            <asp:ListItem>2</asp:ListItem>

            <asp:ListItem>3</asp:ListItem>

            <asp:ListItem>4</asp:ListItem>

            <asp:ListItem></asp:ListItem>

            <asp:ListItem>5</asp:ListItem>

            <asp:ListItem></asp:ListItem>

        </asp:ListBox>

 

    <asp:Button ID="Button1" runat="server" Text="Select" OnClick="Button1_Click" />

   

 <asp:Label ID="Label1" runat="server"></asp:Label>

 

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        foreach (ListItem i in ListBox1.Items)

        {

            if (i.Selected)

            {

                Label1.Text += "<br>" + i.Value;

            }

        }

    }

 

ListBox Control in ASP.Net

We can change List Box appearance

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Font-Bold="True" Font-Italic="True" ForeColor="#FF0066" SelectionMode="Multiple">

        <asp:ListItem>Select</asp:ListItem>

        <asp:ListItem>1</asp:ListItem>

        <asp:ListItem>2</asp:ListItem>

        <asp:ListItem>3</asp:ListItem>

        <asp:ListItem>4</asp:ListItem>

        <asp:ListItem></asp:ListItem>

        <asp:ListItem>5</asp:ListItem>

</asp:ListBox>

ListBox Control in ASP.Net


Don't want to miss updates? Please click the below button!

Follow MindStick