The DataList control features a property named ExtractTemplateRows, described as follows: Gets or sets a value that indicates whether the rows of a Table control, defined in each template of a DataList control, are extracted and displayed. Although I wouldnt say the description is unclear, I find it hard to completely make sense of this property from reading it. To fully understand the role of this property, and how to take advantage of it, you should first think of a subtle difference between the DataList control and its close relativethe Repeater control.
Both controls build their own user interface composing data-bound templates. A template can contain any combination of literal text, ASP.NET controls, and HTML markup. Unlike the Repeater, the DataList control doesnt allow you to open a tag in one template and close it in a subsequent one. Lets consider the following code snippet:
<headertemplate> <table> </headertemplate> <itemtemplate> <tr><td> ... </td></tr> <tr><td> ... </td></tr> </itemtemplate> <footertemplate> </table> </footertemplate>
It defines a table through the use of three distinct templatesHeaderTemplate, ItemTemplate, and FooterTemplate. As you can see, the table is opened in the header and closed in the footer. Each item in the bound data source is rendered through a couple of table rows. These templates work great with a Repeater but not with a DataList. The DataList control forces you to have a full table defined in each template because the control doesnt accept broken tags across templates.
So a DataList ends up having a table in the header and a table for each bound data item. The final result is not a good-looking control. Each table, in fact, is an independent object and formats columns based on the contained data. Misalignment is the foregone conclusion, unless ExtractTemplateRows is used.
ExtractTemplateRows is a Boolean property set to False by default. If you toggle its value on, then the DataList control automatically extracts all the rows from all the tables found in the various templates and builds a unique, all-encompassing table. Basically, the property allows you to create a single table from other smaller tables defined for each DataList template.
The use of the property is subject to a few restrictions. In particular, you
must provide a well-formed Table control (the
The ExtractTemplateRows property is really helpful when you are going to create
quite a complex structure in which tables with different templates are to be
merged together. You can use the ColumnSpan and RowSpan properties of the TableCell
object to control the number of columns and rows the cell spans over.
You should note that when the ExtractTemplateRows property is set to True,
the RepeatColumns, RepeatDirection, and RepeatLayout properties are ignored
and do not affect the appearance of the DataList control.
Whats the advantage that using a DataList with ExtractTemplateRows provides
over using a plain Repeater? The golden rule is that you never use a DataList
if a Repeater is more appropriate. Next, if the DataList is appropriate for
your overall tasks and you still need tables interspersed between tags, resort
to the ExtractTemplateRows property.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant
based in Rome, Italy. Dino is a contributing editor to Windows Developer
Network and MSDN Magazine, and the author of several books for Microsoft
Press including Building Web Solutions with ASP.NET and ADO.NET
and Applied XML Programming for .NET. Contact Dino at [email protected].
<ItemTemplate><br> <asp:table runat="server"><br> <asp:tablerow runat="server"><br> <asp:tablecell runat="server"><br> <%# DataBinder.Eval(Container.DataItem, "Name") %><br> </asp:tablecell><br> </asp:tablerow><br> </asp:table><br></ItemTemplate>