System.Web.UI.WebControls Namespace TemplateField Class
.NET Framework version 2.0
Specifies the template to use for displaying a row of data in a TemplateField control.
<asp:templatefield ... >
<itemtemplate>
... template definition here
</itemtemplate>
</asp:templatefield>
An ITemplate interface that defines how the TemplateField control is rendered when in display ( or read-only ) mode.
The property is read/write with no default value.
Use the ItemTemplate to define the layout of elements in the TemplateField control when displaying a row of data.
To specify a template for displaying a row of data, declare an <ItemTemplate> element between the opening and closing tags of the control. You can then list the contents of the template between the opening and closing <ItemTemplate> ... </ItemTemplate>
tags.
In the ItemTemplate, declare the appropriate HTML or server control you intend to use ( Label, HyperLink, Image, etc. ) and set their data-binding expressions to evaluate to a field in the parent control's data source.
To dispaly data in the ItemTemplate, you must associate a field with a control using the Eval method, as shown in the following example.
<img runat = "server"
src='<%# Eval ( "ProductID", "~/shared/images/gear/{0}.jpg" ) %>'
alt='<%# Eval ( "Model" ) %>'
width=220 height=220 border=0>
This allows the parent control to automatically display the field values in the associated control.
To control the style of the row, use the ItemStyle property of the TemplateField control.
The following examples show several ways of using an ItemTemplate in a TemplateField to display a column with custom layout in a GridView control.
TemplateField Members EditItemTemplate InsertItemTemplate