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"

ListBox-Multiple-Selection

 <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
            <asp:ListItem>Moto</asp:ListItem>
            <asp:ListItem>Samsung</asp:ListItem>
            <asp:ListItem>Micro</asp:ListItem>
            <asp:ListItem>Sony</asp:ListItem>
            <asp:ListItem>AIWA</asp:ListItem>
            <asp:ListItem>APPLE</asp:ListItem>
            <asp:ListItem>Acer</asp:ListItem>
        </asp:ListBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>

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

C#.net 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)
        {

            if(li.Selected)
            {
                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
  Next
 End Sub
End Class

'=======================================================
'Service provided by Telerik (
www.telerik.com)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================

Tags:

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

Author

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 ASP.net 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.