ASP.NET
  Home arrow ASP.NET arrow Page 3 - Using the ASP.NET 2.0 ListBox Control
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Using the ASP.NET 2.0 ListBox Control
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2007-10-29

    Table of Contents:
  • Using the ASP.NET 2.0 ListBox Control
  • Adding Items Programmatically
  • Creating the ListItem objects and adding them using ListBox. Items.Add() method
  • Multiple selections using the ListBox. SelectionMode
  • Using the SqlDataSource control with the ListBox control

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using the ASP.NET 2.0 ListBox Control - Creating the ListItem objects and adding them using ListBox. Items.Add() method


    (Page 3 of 5 )

    As we observed before, the ListBox control has a collection of list items. The items presented by the ListBox control are instances of the ListItem class. The ListItem class is used by other controls, such as the DropDownList and the RadioButtonList controls, to represent a list item and the associated value for that item. The text of a ListItem instance is represented by the ListItem.Text property and the value associated with the item is represented by the ListItem.Value property.

    Usually, this value represents something important to the application and may not be important for the user, or the user doesn't really care about this value. For example, say that you are retrieving an employee id and employee name from the database and populating a ListBox control. You would associate the employee id (through the ListItem.Value) with the employee name (through the ListItem.Text) so the user would see the employee names in the list. When he selects an employee, however, you can do your work using the employee id instead of the employee name by retrieving the Value property of the selected list item.

    In the previous section, we created five ListItem objects, and then we assigned string values to both the ListItem.Text and ListItem.Value properties. Finally, we added those objects to the ListBox control by using the Add() method of the Items Property of the ListBox control. You will find this pattern in many of the controls in the .NET Framework, where you use the Add() method of a property of a collection type to add items to the control. In our case, the ListBox.Items property is of type ListItemCollection, which means that you can use common collection's methods like Add() or Remove() to add or remove items from a ListBox control, and that you can use a foreach statement to iterate over the ListItem objects of the ListBox.Items ListItemCollection property.

    You can think of the Items property as a reference to the ListItemCollection of the ListBox control you are working on; that's why you can call the ListItemCollection.Add() method to add a new list item to the control. If you are not familiar with C# Collections, you must consult other articles about this subject, because it's very important to understand what collections are and how they work.

    The above code could also have been written as follows:

    // checking if this page is loading for the first item or it's

    // postback. If it's not a postback and the page is loading for

    // the first time then we populate the ListBox control with the

    // Listitem objects and if it's a PostBack we simply do nothing

      if (!IsPostBack)

    {

    //creating the ListItem objects and assigning the

    // Text and Value Properties through the Constructor

      ListItem firstItem = new ListItem("Mick Joseph", "10");

      ListItem secondItem = new ListItem("Mary Johnson", "11");

      ListItem thirdItem = new ListItem("David Johnson", "12");

      ListItem FourthItem = new ListItem("Mark Roberts", "13");

      ListItem FifthItem = new ListItem("Paul Bill", "14");

    // adding the ListItem objects to the Item collection of the

      ListBox Control

      ListBox1.Items.Add(firstItem);
     

      ListBox1.Items.Add(secondItem);

      ListBox1.Items.Add(thirdItem);

      ListBox1.Items.Add(FourthItem);

      ListBox1.Items.Add(FifthItem);

    }

    This code uses the ListItem's Constructor that accepts two string values. The first is the Text of the ListItem and the second is the Value of the ListItem.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
     

    ASP.NET ARTICLES

    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT