Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

The Unappreciated ExtractTemplateRows Property


The Unappreciated ExtractTemplateRows Property

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 wouldn’t 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 relative—the 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 doesn’t allow you to open a tag in one template and close it in a subsequent one. Let’s 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 templates—HeaderTemplate, 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 doesn’t 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 tag) for each template you want to include in the output. An exception would be thrown if you specify the table using the HTML <table> element or the ASP.NET HtmlTable control-the <table runat="server"> element. Notice also that all templates must be expressed using the Table control and that only the rows of the tables will be displayed. All other content in the templates is ignored.

<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>

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.

What’s 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].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.