TemplateField.HeaderTemplate Property

Gets or sets the template for displaying the header section of a TemplateField object.

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

public virtual ITemplate HeaderTemplate { get; set; }
/** @property */
public ITemplate get_HeaderTemplate ()

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

public function get HeaderTemplate () : ITemplate

public function set HeaderTemplate (value : ITemplate)

Not applicable.

Property Value

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

Use the HeaderTemplate property to specify the custom content displayed for the header section of a TemplateField object. Define the content by creating a template that specifies how the header section is rendered.

To specify a template, first place opening and closing <HeaderTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <HeaderTemplate> 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 HeaderTemplate property to create a custom template for the header section of a TemplateField field column in a GridView control. The template displays a check box that allows the user to show or hide the rows of the GridView control.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void SelectAllCheckBox_CheckedChanged(Object sender, EventArgs e)
    // Get the CheckBox control that indicates whether to show or 
    // hide the rows in the GridView control. The sender parameter
    // contains the control that raised the event.
    CheckBox showCheckBox = (CheckBox)sender;
    // Show or hide the rows of the GridView control based
    // on the check box value selected by the user.
    if (showCheckBox.Checked)
      ShowRows (true);
      ShowRows (false);
  void ShowRows(bool show)
    // Iterate through the Rows collection of the GridView
    // control and show or hide the rows based on the value
    // of the show parameter.
    foreach(GridViewRow row in AuthorsGridView.Rows)
      row.Visible = show;


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

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a TemplateField column field that contains   -->
      <!-- a CheckBox control in the header section to show or -->
      <!-- hide the rows in the GridView control.              -->
      <asp:gridview id="AuthorsGridView" 
            <headerstyle backcolor="Navy"
              <asp:checkbox id="ShowAllCheckBox"
                text="Show All"
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] 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