ASP.NET
  Home arrow ASP.NET arrow Page 2 - 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 / 9
    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 - Adding Items Programmatically


    (Page 2 of 5 )

    Delete this ListBox control and add another one to the page, or simply clear out the ListItem elements of the control's markup. Check the Enable AutoPostBack checkbox from the task menu as shown in the next screen shot.

    Also set the Width and Height of the ListBox Control through the Properties window (or you can do it through the markup code) as shown below.

    Write the following code in the Page_Load() event handler method.

     // 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

      ListItem firstItem = new ListItem();

       firstItem.Text = "Mick Joseph";

       firstItem.Value = "10";

      ListItem secondItem = new ListItem();

        secondItem.Text = "Mary Johnson";

        secondItem.Value = "11";

      ListItem thirdItem = new ListItem();

      thirdItem.Text = "David Johnson";

       thirdItem.Value = "12";

      ListItem FourthItem = new ListItem();

       FourthItem.Text = "Mark Roberts";

        FourthItem.Value = "13";

      ListItem FifthItem = new ListItem();

       FifthItem.Text = "Paul Bill";

       FifthItem.Value = "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);

    And the markup of the ListBox control looks like this:

    <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
    Height="100px" Width="100px"></asp:ListBox>

    When you run the page you can select another item and the page will post back to the server. For now we don't do anything when posting back. The page looks like the following screen shot:

    Next we'll take a look at what we just did with the above code.

    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

    - Disadvantages of the ASP.NET MVC Framework
    - 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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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