User Id :    Password :      New Member   Forgot Password  
 
List Control in Asp.Net
Description Sometimes we need a lookup (dropdown) from where we can select multiple items. For example, an emplo   No. of Views     2238
  Rating     0
Author Lovleen Rayat   Posted On     15 Aug 2010
Tags ASP.NET,ADO.NET,C#,Collection,LINQ,LINQ to Object,List Control    

Sample Code   Download Code

List 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.

Loading Image.....

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.

Employee
Project

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,

About Author

About Author I am working in a private company for the last 15 months on asp.net technologies. Lovleen
No Photo
 
Country N/A
Company Indus InfoTech
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Rahul Tiwary on 28 Jun 2011 at 05:13 AM
 
very Good
 
 
   
Write your comment here.
Comment
Verification Code