Selection

22 Dec 20222 minutes to read

The ListBox provides support to select an item or a group of item by mouse or keyboard action. There are two selection modes available in list box,

  • Single - To select single item in the list box.
  • Multiple - To select multiple items in the list box.

On selection of each list box item, change event is triggered.

Single selection

To enable single selection in the list box, mode should be set as Single in selectionSettings property.

<ejs-listbox id="listbox1" dataSource="@ViewBag.data">
     <e-listbox-selectionSettings mode="Single"></e-listbox-selectionSettings>
</ejs-listbox>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult selection()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}

Multiple selection

To enable multiple selection in the list box, mode should be set as Multiple in selectionSettings property.

To select multiple items, use the SHIFT, CTRL, and arrow keys to make selections.

NOTE

By default, the selection mode is set as Multiple.

<ejs-listbox id="listbox1" dataSource="@ViewBag.data">
     <e-listbox-selectionSettings mode="Multiple"></e-listbox-selectionSettings>
</ejs-listbox>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult selection()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}

NOTE

To select all the items in the list box, showSelectAll method can also be used.