List Control in Asp.NetList Control
Introduction
Sometimes we need a lookup (dropdown) from where we can select multiple items. For example, an employee can work on a multiple projects at time and if I would have asked to use
DropDownList to show multiple projects, I can’t do so. But this can be possible with
ListBox control.
A
ListBox control is used to display a list of items to the user who can make a single or multiple selections. It is similar to the
DropDownList control with two important differences. One it requires more screen real estate because it always displays a certain number of items in the list and second it allows to select multiple items at a time which is not possible in
DropDownList.
Following are the important properties of the
ListBox control.
SelectionMode --Allows selecting single or multiple items from the list.
Rows -- Specifies the height of the list box
DataSource --Used to set the list of items to show in the list box.
In the example I will show you here how to implement
ListBox control in the C# program. For this I will take the above example of employee.
First we need a collection of
Project object that can be bound to the
ListBox control. Below is the code for the class.
public class Project
{
public string ProjectName { get; set; }
public string EmployeeName { get; set; }
public Project(string ProjName, string EmpName)
{
this.EmployeeName = EmpName;
this.ProjectName = ProjName;
}
}
public static List GetProjectList()
{
List objProjectList = new List();
objProjectList.Add(new Project("CMS", "Loveleen"));
objProjectList.Add(new Project("B2B", "Loveleen"));
objProjectList.Add(new Project("B2B", "Smith"));
objProjectList.Add(new Project("HR Suit", "John"));
objProjectList.Add(new Project("HR Suit", "Loveleen"));
objProjectList.Add(new Project("HR Suit", "Raima"));
objProjectList.Add(new Project("HR Suit", "Ashraf"));
objProjectList.Add(new Project("CMS", "Punit"));
objProjectList.Add(new Project("CMS", "Somya"));
objProjectList.Add(new Project("CMS", "Ashraf"));
return objProjectList;
}
private void FillProject()
{
List objProjectList = GetProjectList();
var ProjectList = from ProjList in objProjectList
select ProjList.ProjectName;
foreach (var ProjectName in ProjectList)
lstBoxProject.Items.Add(new ListItem(ProjectName, ProjectName));
}
Notice that
GetProjectList() method returns a list of project that can be used to bind with Project List Box as needed.
Now in the ASPX page make a dropdownlist for list of employees and a ListBox to show list of projects.
Now if the user selects an employee from the Employee dropdownlist, assigned project will appear in the ListBox control.
protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
List objProjectList = GetProjectList();
if (ddlEmployee.SelectedIndex > 0)
{
var ProjectList = from ProjList in objProjectList
where ProjList.EmployeeName == ddlEmployee.SelectedValue
select ProjList.ProjectName;
foreach (var ProjectName in ProjectList)
lstBoxProject.Items.Add(new ListItem(ProjectName, ProjectName));
}
else
{
FillProject();
}
}
In the example above I have used
LINQ to Object to get the Project list depending on the Employee Name.
Hope it will help you.
Thanks,