ASP.NET Code
  Home arrow ASP.NET Code arrow Page 5 - How to Use the ListBox Control in ASP.NET ...
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 CODE

How to Use the ListBox Control in ASP.NET 2.0
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 105
    2007-02-27

    Table of Contents:
  • How to Use the ListBox Control in ASP.NET 2.0
  • Usage of the ListBox Control in web pages
  • ListBox Examples
  • Dynamic binding to data at run time
  • Databound ListBox Example
  • Steps in converting a Unbound ListBox to a Bound ListBox

  • 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


    How to Use the ListBox Control in ASP.NET 2.0 - Databound ListBox Example


    (Page 5 of 6 )

    In the previous two examples the ListBox was not bound to data at design time (see the design pane). In the following example the ListBox is bound to the DataSource as seen in the design view of this page, BoundList.aspx.

    The source code for this page is as shown here.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server"> 
       <title>Databound Expression</title>
    </head>
    <body>
       <form id="form1" runat="server">
       <div>
         <asp:ListBox ID="ListBox1" runat="server"
           DataSourceID="AccessDataSource1"
           DataTextField="ContactName"
           DataValueField="City"
           AutoPostBack="True" >

         </asp:ListBox><asp:AccessDataSource ID="AccessDataSource1"
           runat="server" DataFile="~/App_Data/nwind.mdb"
           SelectCommand="SELECT [CustomerID], [CompanyName],
               [ContactName],
               [Address], [City], [Phone] FROM [Customers]" >
         </asp:AccessDataSource>

       </div>
         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       </form>
    </body>
    </html>

    There is no code behind to populate the list. The list is bound to data at design time. This is indeed an excellent enhancement for those who do not want to use the code but who are adept at designing using the IDE. There is some code behind but that is only for displaying a selected value in a textbox as show here.

    Partial Class BoundList
       Inherits System.Web.UI.Page

       Protected Sub ListBox1_SelectedIndexChanged(ByVal sender _
       As Object, ByVal e As System.EventArgs) Handles _
       ListBox1.SelectedIndexChanged
         TextBox1.Text = ListBox1.SelectedValue
       End Sub
    End Class

    The following picture shows how this page gets rendered when the button is clicked. Note that the ListBox's Selected value really references the DataFieldValue in the source code.

    More ASP.NET Code Articles
    More By Jayaram Krishnaswamy


       · Declarative binding makes it very easy to work with databases even for those who are...
       · I am using the FormView control to create a form with a number of different type of...
       · Does the stored procedure work in the management studio correctly without...
     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control





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