TemplateField.AlternatingItemTemplate Property

Gets or sets the template for displaying the alternating items in a TemplateField object.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

[TemplateContainerAttribute(typeof(IDataItemContainer), BindingDirection.TwoWay)] 
public virtual ITemplate AlternatingItemTemplate { get; set; }
/** @property */
public ITemplate get_AlternatingItemTemplate ()

/** @property */
public void set_AlternatingItemTemplate (ITemplate value)

public function get AlternatingItemTemplate () : ITemplate

public function set AlternatingItemTemplate (value : ITemplate)

Not applicable.

Property Value

A System.Web.UI.ITemplate-implemented object that contains the template for displaying the alternating items in a TemplateField. The default is a null reference (Nothing in Visual Basic), which indicates that this property is not set.

Use the AlternatingItemTemplate property to specify the custom content displayed for the alternating items in a TemplateField object. Define the content by creating a template that specifies how the alternating items are rendered.


The AlternatingItemTemplate property is commonly used in combination with the ItemTemplate property to create a different appearance for every other item in the data-bound control.

To specify a template, first place opening and closing <AlternatingItemTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <AlternatingItemTemplate> tags. The content can be as simple as plain text or more complex (embedding other controls in the template, for example).

To programmatically access a control defined in a template, first determine which TableCell object in the data-bound control contains the control. Next, use the Controls collection of the TableCell object to access the control. You can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified.

The following code example demonstrates how to use the AlternatingItemTemplate property to create a custom template for the alternating items in a TemplateField field column of a GridView control. The template is a variation of the template specified by the ItemTemplate property, where an image is displayed on the opposite side of the cell.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField AlternatingItemTemplate Example</title>
    <form id="form1" runat="server">
      <h3>TemplateField AlternatingItemTemplate Example</h3>

      <!-- Populate the Columns collection declaratively.    -->
      <!-- Create a TemplateField field column that has both      -->
      <!-- an  item template and an alternating item template.    -->
      <!-- The item template displays an author's image on the    -->
      <!-- left side of the column, while the alternating item    -->
      <!-- template displays an author's image on the right side. -->
      <!-- For this example, the zip field is used for the        -->
      <!-- values of the image URL. For your application, you     -->
      <!-- should use a field that contains valid URLs to         -->
      <!-- images.                                                -->
      <asp:gridview id="AuthorsGridView" 
          <asp:templatefield headertext="Author">
              <asp:image id="LeftAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"  
              <asp:label id="LeftFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
              <asp:label id="LeftLastNameLabel"
                text= '<%# Eval("au_lname") %>'
              <asp:label id="RightFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
              <asp:label id="RightLastNameLabel"
                text= '<%# Eval("au_lname") %>'
              <asp:image id="RightAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [zip] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0