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.

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>
Next: Displayed data from DataReader >>
More ASP.NET Articles
More By Jayaram Krishnaswamy