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>
Next: Drop-Down List >>
More ASP.NET Articles
More By McGraw-Hill/Osborne
|
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.
|
|