ASP.NET
  Home arrow ASP.NET arrow Page 3 - ADO.NET 101: Data Rendering with a Repeate...
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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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

ADO.NET 101: Data Rendering with a Repeater Control Introduction
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-11-16

    Table of Contents:
  • ADO.NET 101: Data Rendering with a Repeater Control Introduction
  • Displaying Data from DataReader with a Repeater Control
  • Configuring the Item
  • Displayed data from DataReader

  • 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


    ADO.NET 101: Data Rendering with a Repeater Control Introduction - Configuring the Item


    (Page 3 of 4 )

    Template Data from a DataReader

    Getting a DataReader to read data from a table in a connected MSDE Server will be considered. The details of reading the data were described in two earlier tutorials, and hence will be summarized here. The present version of VB.NET used for this tutorial does not allow access to SQL 2000 Server, but is limited to MSDE, or MS Access on the same computer. The next picture shows the data link properties of the SQLConnection. Biblio is not one of the standard databases, but it was imported from an MS Access application into MSDE.

    Data Link properties

    The SQLConnection1 on the web form will have the following for its connection string:

    workstation id=XPHTEK;packet size=4096;integrated security=SSPI;data
    source="XPHTEK\TEST"; persist security info=False;initial catalog=Biblio

    A SQLCommand1 placed on the web form will have the following CommandText:

    SELECT FirstName, LastName, Title, Email, Phone FROM Employee

    The HeaderTemplate discussed earlier had these column names in its table cells to properly account for the columns returned by the query.

    Binding the Repeater Control to data

    This is actually performed in two steps. In this first step, the code behind declares and assigns all objects and calls upon the DataReader to access data through the following code. In this step the Repeater's datasource is defined to be that coming from the DataReader as well as the Repeater. It is bound to the data using the DataBind() method:

     Private Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim con As New SqlClient.SqlConnection 'SQLConnection1
    is SQLConnection control 'placed on web form's design pane
    con = SqlConnection1 con.Open() Dim dr As SqlClient.SqlDataReader Dim cmd As New SqlClient.SqlCommand
    'SQLCommand1 is SQLCommand control 'placed on web form's design pane cmd = SqlCommand1 dr = cmd.ExecuteReader() 'Associate the data reader as a 'datasource for the Repeater Repeater1.DataSource = dr
    'Now bind the repeater to data Repeater1.DataBind() con.Close() End Sub

    Designing the ItemTemplate

    Itemtemplate is where the data will be displayed. The actual table cells in this template are shown in the html. For those of you familiar with ASP, this will look like what you would have done to bring in the recordset values to a table cell using the ASP syntax <%...%>, enclosing the databound information. In this case however, you have Framework supported classes making this possible.

    <ItemTemplate>
    <tr>
    <td><%#DataBinder.Eval(Container,"DataItem.FirstName")%></td>
    <td><%#DataBinder.Eval(Container,"DataItem.LastName")%></td>
    <td><%#DataBinder.Eval(Container,"DataItem.Title")%></td>
    <td><%#DataBinder.Eval(Container,"DataItem.Email")%></td>
    <td><%#DataBinder.Eval(Container,"DataItem.Phone")%></td>
    </tr>
    </ItemTemplate>
    

    In order to render the data in the itemtemplate, the DataBinder class is evoked. This class supports RAD (Rapid Application Development) designers to generate, and parse the Data Binding Expression Syntax that you see in the table cells. This is a non-inheritable class with an overloaded method,
    Eval which is used in the table cells.

    The Data Binding Expression creates binding between any property on an ASP.NET page with a data source when the DataBind() method is called. This DataBind() is also applicable to all the iterative controls in addition to any server controls. The DataBinder.Eval static method evaluates the late-bound expressions. Because of late binding the process may run slow. This overloaded method can be used for formatting as well. But this aspect is not used in the Repeater example considered in this tutorial.

    In the Data Binding Expression the Container is the page and the DataItem is the data bound to the RepeaterItem. The expression that is evaluated can be written, for example, in either of the two following ways for the DataItem, Title:

    <%#DataBinder.Eval(Container.DataItem, "Title")%>
    <%#DataBinder.Eval(Container,"DataItem.Title")%>

    The AlternatingItemTemplate

    As mentioned earlier, this is optional and is rendered in display only when defined in the source. The following html shows how this is formatted. It is the same as the Itemtemplate , except the cell color is different.

    <AlternatingItemTemplate>
    <tr>
    <td bgcolor="#ffffcc"><%#DataBinder.Eval(Container,"DataItem.FirstName")%>
    </td>
    <td bgcolor="#ffffcc"><%#DataBinder.Eval(Container,"DataItem.LastName")%>
    </td> <td bgcolor="#ffffcc"><%#DataBinder.Eval(Container,"DataItem.Title")%></td> <td bgcolor="#ffffcc"><%#DataBinder.Eval(Container,"DataItem.Email")%></td> <td bgcolor="#ffffcc"><%#DataBinder.Eval(Container,"DataItem.Phone")%></td> </tr> </AlternatingItemTemplate>

    The FooterTemplate

    This optional template could be put to good use, by bringing in total number of rows, total price, present date and time, etc. In this example, I have just inserted the present time.

    <FooterTemplate>
    <tr>
    <td></td><td></td> <td><%=Now() %></td> <td></td><td></td>
    </tr>
    </table>
    </FooterTemplate>

    More ASP.NET Articles
    More By Jayaram Krishnaswamy


       · This first of the three articles deals with Data bound controls using the VS 2003...
       · Hello, can you please extend your series to "nesting data web controls". Something...
       · This may not be possible as these controls do not support drag and drop...
     

    ASP.NET ARTICLES

    - 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
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway