ASP.NET Listbox Control


The ListBox control is used to create a single- or multi-selection drop-down list.

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            ListBox1.Items.Add("HTML");
            ListBox1.Items.Add("CSS");
            ListBox1.Items.Add("JAVSCRIPT");
            ListBox1.Items.Add("JAVA");
            ListBox1.Items.Add("VB.NET");
            ListBox1.Items.Add("ASP.NET");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "You selected : " + ListBox1.SelectedItem.ToString();
        }

       
        
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ListBox ID="ListBox1" runat="server">
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label">
    
    </div>
    </form>
</body>
</html>

Properties

Property Description
Rows The number of rows displayed in the list
SelectionMode Allows single or multiple selections

ListControl Standard Properties

AppendDataBoundItems, AutoPostBack, CausesValidation, DataTextField, DataTextFormatString, DataValueField, Items, runat, SelectedIndex, SelectedItem, SelectedValue, TagKey, Text, ValidationGroup, OnSelectedIndexChanged
For a full description, go to ListControl Standard Properties

ASP.NET Control Standard Properties

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
For a full description, go to Web Control Standard Attributes.

ASP.NET Web Control Standard Properties

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
For a full description, go to Web Control Standard Attributes.

Example of ListBox Control

Declare one ListBox control in an .aspx file. Then create an event handler for the Click event which displays some text and the selected item, in a Label control.

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            ListBox1.Items.Add("Item 1");
            ListBox1.Items.Add("Item 2");
            ListBox1.Items.Add("Item 3");
            ListBox1.Items.Add("Item 4");
            ListBox1.Items.Add("Item 5");
            ListBox1.Items.Add("Item 6");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "You selected : " + ListBox1.SelectedItem.ToString();
        }

       
        
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ListBox ID="ListBox1" runat="server">
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label">
    
    </div>
    </form>
</body>
</html>

ListBox Control using JavaScript in HTML Code

<script  runat="server">
Sub submit(Sender As Object,e As EventArgs)
mess.Text="You selected " & drop1.SelectedItem.Text
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
   
        <form runat="server">
        <asp:ListBox id="drop1" rows="3" runat="server">
        <asp:ListItem selected="true"> 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 Text="Submit" OnClick="submit" runat="server" />
        <p><asp:label id="mess" runat="server" /></p>
        </form>
   
</body>
</html>

Output


Share this article on