In order for the Repeater control to display data, you must provide two things: a DataSource property value (indicating the source of the data), and at least one HTML template (indicating how the control should display data from its data source).
So that the Repeater can display data (its only reason for existence), you must set the control's DataSource property. You can use a DataTable object (as you'll see in the example later in the chapter) or any other object that supports the IEnumerable interface, including Array, ArrayList, DataView, HashTable, Queue, and more.
The Repeater control provides no built-in support for displaying data. You must tell the control what you want it to do with your data by supplying one or more tem plates for the control's Items collection. Each item in the Items collection renders its data, as you request, and the Repeater iterates through its complete data source using the templates you supply to display data, one row at a time. Table 18.2 lists the data-associated template types that the Repeater control supports. Table 18.3 lists the template types that aren't associated with data.
Table 18.2. Data-Associated Item Types Supported by the Repeater Control
|Item Type ||Description |
|Item ||Created for all rows in the data source, unless you've specified an AlternatingItem template. In that case, it's created for even-numbered, zero-based items. |
|AlternatingItem ||Created for odd-numbered, zero-based items within the data source. |
Table 18.3. Item Types Supported by the Repeater Control That Aren't Associated with Data
|Item Type ||Description |
|Header ||Created for the control header |
|Footer ||Created for the control footer |
|Separator ||Created to separate rows of data |
As ASP.NET renders the Repeater control, the control follows these steps:
The control first looks for a HeaderTemplate element within its body. If it finds one, it outputs the HTML that you've supplied.
The control finds the ItemTemplate element and renders the first row of data using the template it finds.
If the control finds a SeparatorTemplate element, it uses that element's contents to display a separator between rows.
If the control finds an AlternatingItemTemplate element, it uses that template to display the second row of data.
The control repeats the previous three steps for all the rows of data in the data source.
If the control finds a FooterTemplate element, it uses the template to format the footer for the control.
The Repeater control requires at least an ItemTemplate element. It's the only required template for the Repeater control, but the control won't work without this template.
Unlike the DataGrid control, the Repeater control provides absolutely no user interface for laying out its templates. You'll need to venture into the HTML view in order to work with the Repeater control. It's a simple control, but it does require some hand-coding in HTML.
Binding Data in Templates
Using ASP.NET's data binding syntax (that is, surrounding data items with <%# … %> symbols), you can indicate that you want to display data from the Repeater's data source within a template. As ASP.NET renders each template, it provides a logical object, Container, that refers to the RepeaterItem object that represents the template item being displayed. The Container object provides the DataItem property, which refers to the row of data currently being rendered by the control.
The DataBinder object allows ASP.NET to provide data binding as it renders the page, and you'll use the Eval method of the object to perform the data lookup, within the current row, as ASP.NET renders each row of the Repeater's data.
Putting this all together, you'll find expressions such as this one throughout the Repeater's HTML, asking ASP.NET to look up the correct row and column as it renders the page:
<%# Databinder.Eval(Container.DataItem, "CategoryName") %>
Within the body of your page, this expression tells ASP.NET to retrieve the CategoryName field from the data provided in the current row and to output its value into the HTML stream ASP.NET sends to the browser client.
The DataBinder object's Eval method allows you to specify field names as strings, and at runtime, the DataBinder object retrieves the data from the Container.DataItem object (the current row of data) using the field name you've specified as its source.
Hooking Up the Data
When ASP.NET loads your ASPX file, it won't automatically bind the data to your Repeater control. Because you may need control over exactly when and how the data binding occurs, ASP.NET doesn't make assumptions about when you want to retrieve the data.
In order to fill the Repeater, you must take two steps:
Set the DataSource property of the control, as described earlier.
Call the DataBind method of the control. This actually causes ASP.NET to render the control and its data.
You may decide to call the DataBind method only once in the event procedure called when the page loads. You may also want to call DataBind when the data itself has changed, or perhaps you'll want to call it each time you post back to the page.
If you've enabled state management for your page, the Repeater control saves all the required information to re-create its items during postback. You won't need to reset its data source in this case, and the demonstration page takes advantage of this, only binding on the first rendering of the page.