ASP.NET
  Home arrow ASP.NET arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 11
    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


    (Page 1 of 5 )

    The ListBox is a powerful, yet easy to work with, control. In this article we are going to take a look at the ListBox control and learn how to use it.

    The ListBox control is used to give the user a set of items to select from. You may give the user the ability to select just one item or multiple items. As you might have seen before, many of the ASP.NET controls that you place on web pages have a collection of items (as an Item property), such as the ListBox control, the DropDownList control and others. Having a collection of items give you the ability to use the familiar and common methods of collections like Add() and Remove() on that collection; you can also use the foreach statement if your situation requires that.

    The best way to explain how you can create, use, and understand this control is by example. Let's start with a simple example, in which we will be adding the items we want to have in the ListBox using the Properties window at design time.

    Start by creating a new website, and then drag a ListBox control to the Default.aspx page from the Toolbox as shown in the screen shot below.

    The ListBox control will be added to the page with its task menu displayed as shown next. The task menu is a new feature in ASP.NET 2.0 which you will find with many controls. It gives you a list of the operations you can perform on the control or the features you can set on the control.

    Now we need to add a few items so click on Edit Items from the task menu

    The ListItem Collection Editor is shown above. To add an item you need to click on the Add button, which provides you with text boxes to enter the list item text and value as shown in the next screen shot. Type Mick Joseph in the Text property,  10 in the Value property and True for the Selected property.

    Now click on the add button to add this item. The markup of the control looks like the following after adding one item.

    <asp:ListBox ID="ListBox1" runat="server">

    <asp:ListItem Selected="True" Value="10">Mick
    Joseph</asp:ListItem>

    </asp:ListBox></div>

    Continue adding the following items in the same way we added the first item:

    Mary Johnson with value of 11
    David Johnson with value of 12
    Mark Roberts with value of 13
    Paul Bill with value of 14

    Now the markup of the control would look like this:

    <asp:ListBox ID="ListBox1" runat="server">

    <asp:ListItem Selected="True" Value="10">Mick
    Joseph</asp:ListItem>

    <asp:ListItem Value="11">Mary Johnson</asp:ListItem>

    <asp:ListItem Value="12">David Johnson</asp:ListItem>

    <asp:ListItem Value="13">Mark Roberts </asp:ListItem>

    <asp:ListItem Value="14 ">Paul Bill </asp:ListItem>

    </asp:ListBox>

    As you can see in the markup, the list items are added as child elements to the ListBox control. If you run the page you would get the following HTML code for this control:

    <select size="4" name="ListBox1" id="ListBox1">

     <option selected="selected" value="10">Mick Joseph</option>

      <option value="11">Mary Johnson</option>

      <option value="12">David Johnson</option>

      <option value="13">Mark Roberts </option>

      <option value="14 ">Paul Bill </option>

     <option value=""></option>

    </select>

    The ListBox control has been rendered as an html <select> element.

    The items you have added to the ListBox control are actually added to its Items property which is of type ListItemCollection. The ListItemCollection stores objects of type ListItem; that's why you see the elements <asp:ListItem> that represent each item in the list.

    Let's try another example that adds those items programmatically in the Page_Load() event handler method.

    More ASP.NET Articles
    More By Michael Youssef


       · Hi guys,Your comments are welcome.
       · Hi,I need listBox with images and text same as ToolBox
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek