.NET Framework Class Library
TemplateField..::.FooterTemplate Property

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

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

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
<TemplateContainerAttribute(GetType(IDataItemContainer))> _
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> _
Public Overridable Property FooterTemplate As ITemplate
Visual Basic (Usage)
Dim instance As TemplateField
Dim value As ITemplate

value = instance.FooterTemplate

instance.FooterTemplate = value
C#
[BrowsableAttribute(false)]
[TemplateContainerAttribute(typeof(IDataItemContainer))]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
public virtual ITemplate FooterTemplate { get; set; }
Visual C++
[BrowsableAttribute(false)]
[TemplateContainerAttribute(typeof(IDataItemContainer))]
[PersistenceModeAttribute(PersistenceMode::InnerProperty)]
public:
virtual property ITemplate^ FooterTemplate {
    ITemplate^ get ();
    void set (ITemplate^ value);
}
JScript
public function get FooterTemplate () : ITemplate
public function set FooterTemplate (value : ITemplate)

Property Value

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

Use the FooterTemplate property to specify the custom content displayed in the footer section of a TemplateField object. Define the content by creating a template that specifies how the footer section is rendered.

To specify a template, first place opening and closing <FooterTemplate> tags between the opening and closing tags of the <TemplateField> element. Next, add the custom content between the opening and closing <FooterTemplate> 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.

Examples

The following code example demonstrates how to use the FooterTemplate property to create a custom template for the footer section of a TemplateField field column in a GridView control. The template displays the sum of the values in the TemplateField field column.

Visual Basic
<%@ 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">

  ' Create a variable to store the order total.
  Private orderTotal As Decimal = 0.0

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

    If e.Row.RowType = DataControlRowType.Footer Then

      ' Get the OrderTotalLabel Label control in the footer row.
      Dim total As Label = CType(e.Row.FindControl("OrderTotalLabel"), Label)

      ' Display the grand total of the order formatted as currency.
      If (Not total Is Nothing)

        total.Text = orderTotal.ToString("c")

      End If 

    End If

  End Sub

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

    If e.Row.RowType = DataControlRowType.DataRow Then

      ' Get the cell that contains the item total.
      Dim cell As TableCell = e.Row.Cells(2)

      ' Get the DataBoundLiteralControl control that contains the 
      ' data bound value.
      Dim boundControl As DataBoundLiteralControl = CType(cell.Controls(0), DataBoundLiteralControl)

      ' Remove the '$' character so that the type converter works properly.
      Dim itemTotal As String = boundControl.Text.Replace("$",  "")

      ' Add the total for an item (row) to the order total.
      orderTotal += Convert.ToDecimal(itemTotal)

    End If

  End Sub

</script>

<html  >
  <head runat="server">
    <title>TemplateField FooterTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TemplateField FooterTemplate 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="OrderGridView" 
        datasourceid="OrderSqlDataSource" 
        autogeneratecolumns="False" 
        showfooter="true"
        onrowcreated="OrderGridView_RowCreated"
        onrowdatabound="OrderGridView_RowDataBound"   
        runat="server">

        <columns>

          <asp:boundfield datafield="UnitPrice"
            itemstyle-horizontalalign="Right"
            headertext="Unit Price" 
            dataformatstring="{0:c}"/>

          <asp:boundfield datafield="Quantity"
            itemstyle-horizontalalign="Right"
            headertext="Quantity"/>

          <asp:templatefield headertext="Total"
            itemstyle-horizontalalign="Right"
            footerstyle-horizontalalign="Right"
            footerstyle-backcolor="Blue"
            footerstyle-forecolor="White">
            <itemtemplate>
              <%#Eval("Total", "{0:c}") %>
            </itemtemplate>
            <footertemplate>
              <asp:label id="OrderTotalLabel"
                runat="server"/>
            </footertemplate>
          </asp:templatefield>

        </columns>

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Northwind sample database.                   -->
      <asp:sqldatasource id="OrderSqlDataSource"  
        selectcommand="SELECT [OrderID], [UnitPrice], [Quantity], [UnitPrice]*[Quantity] As [Total] FROM [order details] WHERE [OrderID]=10248"
        connectionstring="server=localhost;database=northwind;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

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

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

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

  // Create a variable to store the order total.
  private Decimal orderTotal = 0.0M;

  void OrderGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    if (e.Row.RowType == DataControlRowType.Footer)
    {

      // Get the OrderTotalLabel Label control in the footer row.
      Label total = (Label)e.Row.FindControl("OrderTotalLabel");

      // Display the grand total of the order formatted as currency.
      if (total != null)
      {
        total.Text = orderTotal.ToString("c");
      }

    }

  }

  void OrderGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

      // Get the cell that contains the item total.
      TableCell cell = e.Row.Cells[2];

      // Get the DataBoundLiteralControl control that contains the 
      // data-bound value.
      DataBoundLiteralControl boundControl = (DataBoundLiteralControl)cell.Controls[0];

      // Remove the '$' character so that the type converter works properly.
      String itemTotal = boundControl.Text.Replace("$",  "");

      // Add the total for an item (row) to the order total.
      orderTotal += Convert.ToDecimal(itemTotal);

    }

  }

</script>

<html  >
  <head runat="server">
    <title>TemplateField FooterTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TemplateField FooterTemplate 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="OrderGridView" 
        datasourceid="OrderSqlDataSource" 
        autogeneratecolumns="False" 
        showfooter="true"
        onrowcreated="OrderGridView_RowCreated"
        onrowdatabound="OrderGridView_RowDataBound"   
        runat="server">

        <columns>

          <asp:boundfield datafield="UnitPrice"
            itemstyle-horizontalalign="Right"
            headertext="Unit Price" 
            dataformatstring="{0:c}"/>

          <asp:boundfield datafield="Quantity"
            itemstyle-horizontalalign="Right"
            headertext="Quantity"/>

          <asp:templatefield headertext="Total"
            itemstyle-horizontalalign="Right"
            footerstyle-horizontalalign="Right"
            footerstyle-backcolor="Blue"
            footerstyle-forecolor="White">
            <itemtemplate>
              <%#Eval("Total", "{0:c}") %>
            </itemtemplate>
            <footertemplate>
              <asp:label id="OrderTotalLabel"
                runat="server"/>
            </footertemplate>
          </asp:templatefield>

        </columns>

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Northwind sample database.                   -->
      <asp:sqldatasource id="OrderSqlDataSource"  
        selectcommand="SELECT [OrderID], [UnitPrice], [Quantity], [UnitPrice]*[Quantity] As [Total] FROM [order details] WHERE [OrderID]=10248"
        connectionstring="server=localhost;database=northwind;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

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

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker