ASP.NET
  Home arrow ASP.NET arrow Page 3 - 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 - A Closer Look at Templates


    (Page 3 of 4 )

    The ItemTemplate for the Repeater control is just one type of template. Templates can contain data retrieved from the DBMS, HTML tags, and inline ASP.NET statements. There are five templates that can be used with the Repeater control:

    • HeaderTemplate  Used to format the header section of the Repeater control
    • ItemTemplate  Used to display and format data displayed in the Repeater control
    • AlternatingItemTemplate  Used to display and format alternate data items
    • SeparatorTemplate  Used to separate data displayed by the Repeater control
    • FooterTemplate  Used to format the footer section of the Repeater control

    The following example is a modification of our previous example and illustrates how to use these different templates in an application. All rows and columns are retrieved from the Customers table. We’ll display the CustomerFirstName and CustomerLastName columns in a table.

    The HeaderTemplate is the first template used in this example. The HeaderTemplate contains HTML tags that create a table with two columns: Customer First Name and Customer Last Name.

    The ItemTemplate appears next. This is nearly identical to the ItemTemplate in the previous example; however, besides having it access data from the DBMS, we’ve also included HTML tags that define a row and columns of the table. The data is displayed in a blue font.

    The AlternatingItemTemplate defines the format for alternating items that appear in the Repeater control, that is, the second, fourth, and so on items. The format is the same as for the ItemTemplate; however, the data appears in red instead of blue.

    The FooterTemplate is the last template in this example and contains the HTML closing tag for the table.

    <%@ 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=CustomerContact Data")
     
    cmdSelectRows = New SqlCommand( "Select * From Customers", 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">
            
    <HeaderTemplate>
               
    <table border=1 cellpadding=5>
                <tr>
                 
    <th>Customer First Name</th>
                 
    <th>Customer Last Name</th>
               
    </tr>
            
    </HeaderTemplate>
            
    <ItemTemplate>
              
    <font color="blue">
               <tr>
                
    <td><%# Container.DataItem("custFirstName") %></td>
                 <td><%# Container.DataItem("custLastName") %></td>
              
    </tr>
            
    </ItemTemplate>
            
    <AlternatingItemTemplate>
              
    <font color="red">
               
    <tr>
                
    <td><%# Container.DataItem("CustFirstName") %></td>
                 <td><%# Container.DataItem("CustLastName") %></td>
               </tr>
            
    </AlternatingItemTemplate>
            
    <FooterTemplate>
               
    </table>
            
    </FooterTemplate>
           
    </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