Tuesday, December 19, 2006

Repeater应该不算是和DataGrid和DataList一路的,只不过看上去有一点像而已。

DataGrid/GridView主要是用来显示数据库报表类的数据的,或者说就是用来显示报表的。

DataList(我认为应该叫做ListView)可以用来显示“格子中自有天地”的数据。比如Blog的comments就可以用DataList来做。

A Brief Overview of the DataGrid Web Control


The DataGrid Web control was designed to display data in an HTML

. Each row in the DataGrid's DataSource is displayed as a row in the HTML
. The DataGrid has an AutoGenerateColumns property, which can be set to either True or False. If AutoGenerateColumns is set to True (the default), each field in the DataSource is displayed as a column in the resulting HTML
. If, however, AutoGenerateColumns is set to False, then the developer must explicitly specify what columns should appear.

One downside of a DataGrid is its rather "blocky" display. That is, the DataGrid displays each DataSource record as a row in an HTML

, and each field as a column in said table. While the DataGrid allows for the developer to customize the output for a particular column through the , it still is restrictive in that each DataSource record occupies precisely one HTML
row.

Despite its limitations on specifying the display of the DataGrid's data, the DataGrid is the most commonly used data Web control due to its impressive feature list.

A Brief Overview of the DataList Web Control


The DataList Web control is useful for displaying data that can be highly customized in its layout. By default, the DataList displays its data in an HTML

. However, unlike the DataGrid, with the DataList you can specify via the RepeatColumns how many DataSource records should appear per HTML
row.

RepeatColumns="5" >          Question:
<%# DataBinder.Eval(Container.DataItem, "Description") %>

Views:
<%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:#,###}") %>

[View a Live Demo!]

As the code example above shows, DataLists are comprised of a number of templates. Templates can contain a mix of HTML syntax and databinding expressions, as shown in the above ItemTemplate. Databinding expressions are ones delimited by <%# and %>, and contain code that's executed when the DataListItem's DataBind() method is called. The ItemTemplate specifies the template used for each record in the DataSource. The DataList contains other templates, which are listed below:

  • AlternatingItemTemplate - if specified, each alternating item in DataSource uses this template instead of the ItemTemplate.
  • EditItemTemplate - the template used when a DataList row is in "edit mode".
  • HeaderTemplate - the template for the DataList's header item. Precisely one header item is created for the DataList item if the DataList's ShowHeader property is set to True.
  • FooterTemplate - the template for the DataList's footer item. Precisely one footer item is created for the DataList item if the DataList's ShowFooter property is set to True.
  • SeparatorTemplate - this template, if specified, is applied after each DataListItem has been added.

The DataList is capable of performing sorting, paging, and editing of its data, but to do so requires quite a bit more programming than to accomplish these tasks with the DataGrid. Therefore, if you know you will be needing these functionalities, it is likely best to proceed with the DataGrid. If, however, you will not need these functionalities, but do necessarily need the extra control over the formatting, consider a DataList.

A Brief Overview of the Repeater Control


The Repeater control, unlike the DataGrid and DataList, is not derived from the WebControl class, and therefore does not have the stylistic properties that are common to all Web controls. (These stylistic properties that are common to all Web controls include Font, ForeColor, BackColor, BorderStyle, and so on.)

The Repeater, like the DataList, only supports templates; however, the Repeater has only a subset of the DataList's template options. Specifically, the following templates can be used with the Repeater:

  • AlternatingItemTemplate,
  • ItemTemplate,
  • HeaderTemplate,
  • FooterTemplate, and
  • SeparatorTemplate

The Repeater control provides the maximum amount of flexibility over the HTML produced. Whereas the DataGrid wraps the DataSource contents in an HTML

, and the DataList wraps the contents in either an HTML
or tags , the Repeater adds absolutely no HTML content other than what you explicitly specify in the templates.

Therefore, the Repeater is a good data Web control choice if you want to display data in, say, an unordered list. As the following code and live demo show, displaying database data in an unordered list using a Repeater is relatively straightforward. All you have to do is add the

    tag to the HeaderTemplate, the
tag to the FooterTemplate, and an
  • tag along with the DataSource field you want to display to the ItemTemplate:

  •           
    • <%# DataBinder.Eval(Container.DataItem, "Description")%>
    [View a Live Demo!]

    The Repeater is a good choice if you need to display your data in a format different than an HTML

    . Unfortunately, the Repeater does not provide any built-in means for editing, sorting, or paging of data. However, these mechanisms could be added programmatically, but would result in a lot of code and effort.

    Conclusion

    This article examined the similarities and differences of the three data Web controls: the DataGrid, DataList, and Repeater. The main similarities between these three controls is in how they iterate through their DataSource, building up a collection of DataWebControlNameItems. Furthermore, the three Web controls all share three events: ItemCreated, ItemDataBound, and ItemCommand.

    Each of the data Web controls has its own strengths and weaknesses. The DataGrid, for example, is great for quickly and easily displaying database data in an HTML

    , and allows for advanced features such as paging, sorting, and in-place editing to be added with little to no programming. However, the DataGrid is quite limited in the general format with which the data is presented.

    The DataList allows for more freedom. For example, in an earlier live demo we saw that using the DataList's RepeatColumns property, multiple DataSource records could be displayed in a single HTML

    row. Additionally, the DataList's content is specified via templates, which allows for a high degree of customization.

    The Repeater allows for the utmost flexibility in its output, since the only HTML rendered from a Repeater is the HTML generated in its templates. That is, no additional HTML output is generated, as is the case with both the DataGrid and DataList. The Repeater, however, does not have any built-in support for sorting, paging, or editing of its data.