Export (0) Print
Expand All

TemplateField.ItemTemplate Property

Gets or sets the template for displaying an item in a data-bound control.

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

'Declaration
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> _
<TemplateContainerAttribute(GetType(IDataItemContainer), BindingDirection.TwoWay)> _
<BrowsableAttribute(False)> _
Public Overridable Property ItemTemplate As ITemplate

Property Value

Type: System.Web.UI.ITemplate
A System.Web.UI.ITemplate-implemented object that contains the template for displaying an item in a TemplateField. The default is Nothing, which indicates that this property is not set.

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

NoteNote

You can optionally define the AlternatingItemTemplate property 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 <ItemTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <ItemTemplate> 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 ItemTemplate property to create a custom template for the items of a TemplateField field in a GridView control. The template displays the field's value in a RadioButtonList control.


<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub TitleGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    ' Get the RadioButtonList control from the row.
    Dim radio As RadioButtonList = CType(e.Row.FindControl("TypeList"), RadioButtonList)

    ' Select the appropriate option button based on the value
    ' of the Type field for the row. In this example, the Type
    ' field values are stored in the column in the 
    ' GridView control.
    If Not radio Is Nothing Then

      Select Case e.Row.Cells(3).Text.Trim()

        Case "business"
          radio.SelectedIndex = 0

        Case "mod_cook"
          radio.SelectedIndex = 1

        Case "popular_comp"
          radio.SelectedIndex = 2

        Case "psychology"
          radio.SelectedIndex = 3

        Case "trad_cook"
          radio.SelectedIndex = 4

        Case Else
          radio.SelectedIndex = 5

      End Select

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField ItemTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TemplateField ItemTemplate Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="TitleGridView" 
        datasourceid="TitleSqlDataSource" 
        autogeneratecolumns="false"
        onrowdatabound="TitleGridView_RowDataBound" 
        runat="server">

        <columns>

          <asp:boundfield datafield="title"
            headertext="Title"/>

          <asp:boundfield datafield="price"
            dataformatstring="{0:c}"
            headertext="Price"/>  

          <asp:templatefield headertext="Type">
            <itemtemplate>
              <asp:radiobuttonlist id="TypeList"
                datasourceid="TypeSqlDataSource"
                datatextfield="type"
                enabled="false"  
                runat="server"/>  
            </itemtemplate>
          </asp:templatefield>

          <asp:boundfield datafield="type"/>

        </columns>

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="TitleSqlDataSource"  
        selectcommand="SELECT [title], [price], [type] FROM [titles]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

      <asp:sqldatasource id="TypeSqlDataSource"  
        selectcommand="SELECT Distinct [type] FROM [titles]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft