Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [225]

By Root 5716 0
that, iterative controls differ from each other in terms of layout capabilities and functionality.

Iterative controls differ from list controls because of their greater rendering flexibility. An iterative control lets you apply an ASP.NET template to each row in the bound data source.

A list control, on the other hand, provides a fixed and built-in template for each data item. List controls are customizable to some extent, but you can’t change anything other than the text displayed. No changes to layout are supported. On the other hand, using a list control is considerably easier than setting up an iterative control, as you’ll see in a moment. Defining templates requires quite a bit of declarative code, and if accomplished programmatically, it requires that you write a class that implements the ITemplate interface. A list control requires only that you go through a few data-binding properties.

Meanwhile, let’s briefly meet each control. When they are properly customized and configured, there’s no graphical structure—be it flat or hierarchical—that Repeater and DataList controls can’t generate.

The Repeater Control


The Repeater displays data using user-provided layouts. It works by repeating a specified ASP.NET template for each item displayed in the list. The Repeater is a rather basic templated data-bound control. It has no built-in layout or styling capabilities. All formatting and layout information must be explicitly declared and coded using HTML literals, CSS classes, and ASP.NET controls.

Table 10-4 lists the main properties exposed by the control, not counting those inherited from the base class WebControl.

Table 10-4. Properties of the Repeater Control

Property

Description

AlternatingItemTemplate

Template to define how every other item is rendered.

DataMember

The name of the table in the DataSource to bind.

DataSource

The data source that populates the items of the list.

DataSourceID

ID of the data source component to provide data.

FooterTemplate

Template to define how the footer is rendered.

HeaderTemplate

Template to define how the header is rendered.

Items

Gets a RepeaterItemCollection object—that is, a collection of RepeaterItem objects. Each element of the collection represents a displayed data row in the Repeater.

ItemTemplate

Template to define how items are rendered.

SeparatorTemplate

Template to define how the separator between items is to be rendered.

For the most part, properties are the template elements that form the control’s user interface. The Repeater populates the Items collection by enumerating all the data items in the bound data source. For each data-bound item (for example, a table record), it creates a RepeaterItem object and adds it to the Items collection. The RepeaterItemCollection class is a plain collection class with no special or peculiar behavior. The RepeaterItem class represents a displayed element within the overall structure created by the Repeater. The RepeaterItem contains properties to point to the bound data item (such as a table record), the index, and the type of the item (regular item, alternating item, header, footer, and so on). Here’s a quick example of a Repeater:

We have customers in the following cities



<%# Eval("City")%>

  

<%# Eval("Country")%>


<%# CalcTotal() %> cities

Bound to the output of the following method call, the structure produces what’s shown in Figure 10-5:

// Currently selected country name

var country = Countries.SelectedValue;

// Make a call to the DAL to grab cities and countries

var repo = new CustomerRepository();

var data = repo.GetCitiesWithCustomers(country);

// Bind

Repeater1.DataSource = data;

Repeater1.DataBind();

The method on the data access layer ends up placing a SQL query like

Return Main Page Previous Page Next Page

®Online Book Reader