Listbox select multiple

Listbox select multiple

In this post we will select multiple list items and iterate it using Foreach loop. To enable the listBox multiple selection you need to add an attribute SelectionMode="Multiple"


 <form id="form1" runat="server">
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>

To do listbox selection press / hold Ctrl key and use mouse pointer to select list items. code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
        foreach(ListItem li in ListBox1.Items)

                Response.Write(li.Text + "<BR/>");


VB.Net code

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Partial Class _Default
 Inherits System.Web.UI.Page
 Protected Sub Page_Load(sender As Object, e As EventArgs)

 End Sub
 Protected Sub Button1_Click(sender As Object, e As EventArgs)
  For Each li As ListItem In ListBox1.Items

   If li.Selected Then
    Response.Write(li.Text + "<BR/>")

   End If
 End Sub
End Class

'Service provided by Telerik (
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik


Listbox select multipleAspnet Listbox Selectedindexchanged problems and solutionHow to select multiple columns in Linq- codelistbox select eventHow to get Jquery selected optionLinq Group By Multiple ColumnsJquery Dropdown Selected ValueGet Selected Value From Dropdownlist [JQuery]ListBox Binding csharpJQuery Select add listBox items


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.