TemplateField.AlternatingItemTemplate Property

Note: This property is new in the .NET Framework version 2.0.

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)

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.

NoteNote

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#" %>

<html>
  <body>
    <form 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" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">
                
        <columns>
                
          <asp:templatefield headertext="Author">
            <itemtemplate>
              <asp:image id="LeftAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"  
                runat="server"/>
              <asp:label id="LeftFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="LeftLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
            </itemtemplate>
            <alternatingitemtemplate>
              <asp:label id="RightFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="RightLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
              <asp:image id="RightAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"
                runat="server"/>
            </alternatingitemtemplate>
          </asp:templatefield>
                
        </columns>
                
      </asp:gridview>
            
      <!-- 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"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>


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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show: