ASP.NET
  Home arrow ASP.NET arrow Page 2 - Binding Data to Controls
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

Binding Data to Controls
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-04-27

    Table of Contents:
  • Binding Data to Controls
  • The Repeater Control
  • A Closer Look at Templates
  • Drop-Down List

  • 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


    Binding Data to Controls - The Repeater Control


    (Page 2 of 4 )

    The Repeater control is used to display records from a database and is declared by using the following ASP.NET tags. The <asp:Repeater> tag requires two attributes. These are ID and Runat, both of these you’ve learned about throughout this book. The ID attribute uniquely identifies the Repeater, and the Runat attribute specifies that this control runs on the server.

    Within the Repeater tag is the ItemTemplate. The ItemTemplate specifies what is to be displayed. Here you reference data returned from the database. (You’ll see how to retrieve data later in this section.)

    The ItemTemplate tag in turn contains a data binding expression, which is used to reference this data. This expression begins with <%# and ends with %>. The expression itself calls the DataItem() method of the Container object and passes it the column name that identifies the column from the bound data source that the control displays. A Container object is an object that contains other objects.

    <asp:Repeater ID="RepeaterID"  Runat="Server">
      
    <ItemTemplate>
         
    <%# Container.DataItem("ColumnName") %>
      
    </ItemTemplate>
    </asp:Repeater>

    Data is retrieved from the database using techniques that you learned in Chapter 10. You’ll recall that you need to open a connection to the database and then pass the database management system (DBMS) a query. A reader is then used to access the result returned by the DBMS.

    Data that is retrieved from the DBMS must be bound to the Repeater control. This is accomplished by assigning a reference to the reader to the DataSource property of the Repeater control, and then by calling the Repeater control’s DataBind() method. Every control has a DataBind() function that binds (links) the data source to the control. The control then displays the data once the data source is bound to the control.

    Connecting to the database, running the query, and binding the control to the data source typically occurs once in your application. It makes sense to do this when the page is loaded. Therefore, place the code that links to the data in the Page_Load subroutine as shown here. This example connects to the Microsoft SQL Server database, but you can connect to another database, as illustrated in Chapter 10.

    This example begins by declaring SqlConnection, SqlCommand, and SqlData-Reader variables. Next, a connection is opened to the DBMS by creating a    
    SqlConnection object and passing it login information.

    An instance of the SqlCommand object is then created, passing it the query that will be sent to the DBMS to retrieve our data. The connection is then opened by calling the Connection object’s Open() method, and data is returned to the reader by calling the SqlCommand object’s ExecuteReader() method.

    A reference to the reader is assigned to the DataSource property of the Repeater control, and then the DataBind() method is called to bind the data to the Repeater control. The reader and the connection are then closed.

    Sub Page_Load
      
    Dim conCust As SqlConnection
     
    Dim cmdSelectRows As SqlCommand
     
    Dim dtrCust As SqlDataReader
     
    conCust = New SqlConnection( "Server=server;UID=userID;
             
    PWD=password;
             
    Database=database" )
      cmdSelectRows = New SqlCommand( "query", conCust)
     
    conCust.Open()
     
    dtrCust = cmdSelectRows.ExecuteReader()
      RepeaterControlID
    .DataSource = dtrCust
      RepeaterControlID.DataBind()
     
    dtrCust.Close()
     
    conCust.Close()
    End Sub

    Let’s assemble these pieces and retrieve data from the Customers table that you created in Chapter 11. The following code shows how this is done. We begin by defining the Page_Load subroutine. The database is called CustomerContactData. Replace MyID and MyPassword with your user ID and password. We’ll use a very simple query that retrieves all the rows and all the columns from the Customers table.

    Next, we create a form on our web page that contains the Repeater control. The Repeater control has a data binding expression that calls the DataItem() of the Container object to access the CustomerLastName column of the data returned to the application by the DBMS in response to our query.

    <%@ Import Namespace="System.Data.SqlClient" %>
    <Script Runat="Server">
    Sub Page_Load
     
    Dim conCust As SqlConnection
     
    Dim cmdSelectRows As SqlCommand
     
    Dim dtrCust As SqlDataReader
     
    conCust = New SqlConnection(
      "Server=localhost; UID=MyID;PWD=MyPassword; Database=CustomerContactData" )

      cmdSelectRows = New SqlCommand( "Select * From custContact", conCust)
     
    conCust.Open()
     
    dtrCust = cmdSelectRows.ExecuteReader()
      rptCust.DataSource = dtrCust
     
    rptCust.DataBind()
     
    dtrCust.Close()
     
    conCust.Close()
    End Sub
    </Script>
    <html>
       
    <head><title>Repeater Control Data Binding</title></head>
       <body>
         
    <form Runat="Server">
           
    <asp:Repeater ID="rptCust" Runat="Server">
              
    <ItemTemplate>
                 
    <%# Container.DataItem("custLastName") %>
              
    </ItemTemplate>
            
    </asp:Repeater>
          
    </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 1 hosted by Hostway
    Stay green...Green IT