How to use Listbox control in ASP.Net

In this article I will explain you how to Select multiple item  in Listbox control in asp.net using c#.

//Design Source Code
<%@ PageLanguage=”C#”AutoEventWireup=”true”CodeFile=”Default.aspx.cs”Inherits=”_Default”%>
<!DOCTYPE htmlPUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
    <title>ListBox Example</title>
    
</head>
<body>
    <form id=”form1″ runat=”server”>
    
    <div>
   
     <asp:ListBox id=”ListBox1″
       Rows=”5″
       width=”150px”
       SelectionMode=”Multiple”
       runat=”server”>
        <asp:ListItem>ASP.Net </asp:ListItem>
        <asp:ListItem>C# .Net </asp:ListItem>
        <asp:ListItem>VB.Net </asp:ListItem>
        <asp:ListItem>HTML </asp:ListItem>
        <asp:ListItem>JAVA </asp:ListItem>
        </asp:ListBox>
       
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Show” Width=”64px” />
       
        <br/>
        <br/>
       
        <asp:Label ID=”Label1″ runat=”server”/>
              
      
    </div>
    </form>
</body>
</html>
//C# Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partialclass _Default: System.Web.UI.Page
{
    protected voidPage_Load(object sender, EventArgs e)
    {
    }
    protected voidButton1_Click(object sender, EventArgs e)
    {
       
        foreach (ListItemlv in ListBox1.Items)
        {
            if (lv.Selected)
            {
                Label1.Text += lv.Text +“<br/>”;
            }
        }
      
        }
   
}

Hold the Control key when selecting multiple item in listbox

One Comment on “How to use Listbox control in ASP.Net”

Leave a Reply

Your email address will not be published. Required fields are marked *