ASP.NET
  Home arrow ASP.NET arrow Page 2 - Learning More About Binding Data to Contro...
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

Learning More About Binding Data to Controls
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-05-04

    Table of Contents:
  • Learning More About Binding Data to Controls
  • List Box
  • Hyperlinks
  • Quiz

  • 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


    Learning More About Binding Data to Controls - List Box


    (Page 2 of 4 )

    The List Box control is bound to data much as you bind data to the drop-down list box, which you learned how to do earlier in this chapter. The actions of connecting to the database, sending the query, and binding data occur within the Page_Load subroutine. Statements required to execute these tasks should be enclosed within an If statement that executes if the IsPostBack property is false, which means that the page is being loaded for the first time (see the earlier section “Drop-Down List”).

    The following example illustrates how to bind data to a list box control. In this example we’re populating the list box with the CustomerLastName column from the Customers table. Notice that once again the column name is used in the Page_Load subroutine to bind the custLastName to the DataTextField property of the list box.

    The web page consists of a form that contains the list box control. Only the list box control is used in this example (Figure 12-2). You can insert other controls (see Chapter 8) after you are comfortable binding data to the list box.


    Figure 12-2.  The list box contains last names from the database.

    <%@ Import Namespace="System.Data.SqlClient" %>
    <Script Runat="Server">
    Sub Page_Load
     
    If Not IsPostBack Then
       
    Dim conCust As SqlConnection
       
    Dim cmdSelectRows As SqlCommand
       
    Dim dtrCust As SqlDataReader
       
    conCust = New SqlConnection( "Server=localhost;UID=
    MyID;PWD=MyPassword;Database=CustomerContact Data")
       
    conCust.Open()
       
    cmdSelectRows = New SqlCommand("Select custLastName From
    custContact", conCust)
       
    dtrCust = cmdSelectRows.ExecuteReader()
        lstCustomerLastName.DataSource = dtrCust
       
    lstCustomerLastName.DataTextField = "custLastName"
       
    lstCustomerLastName.DataBind()
        dtrCust.Close()
       
    conCust.Close()
     
    End If
    End Sub
    </Script>
    <html>
      
    <head><title>List Box Control Data Binding</title></head>
      
    <body>
         
    <form Runat="Server">
            
    <asp:ListBox ID="lstCustomerLastName" Runat="Server" />
         
    </form>
      
    </body>
    </html>

    More ASP.NET Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "ASP.NET 2.0 DeMYSTiFieD," published by...
     

    Buy this book now. This article is excerpted from chapter 12 of ASP.NET 2.0 DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne; ISBN: 0072261412). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - 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

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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