TemplateField.FooterTemplate 属性

定义

获取或设置用于显示 TemplateField 对象的脚注部分的模板。

public:
 virtual property System::Web::UI::ITemplate ^ FooterTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer))]
public virtual System.Web.UI.ITemplate FooterTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer))>]
member this.FooterTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property FooterTemplate As ITemplate

属性值

ITemplate

一个实现了 ITemplate 的对象,该对象包含用于显示 TemplateField 的脚注部分的模板。 默认值为 null,指示未设置此属性。

属性

示例

下面的代码示例演示如何使用FooterTemplate属性为控件中GridView字段列的TemplateField页脚部分创建自定义模板。 该模板显示字段列中的值 TemplateField 的总和。


<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

注解

使用 FooterTemplate 属性指定对象页脚节 TemplateField 中显示的自定义内容。 通过创建一个模板来定义内容,该模板指定页脚节的呈现方式。

若要指定模板,请先在元素的<TemplateField>开始标记和结束标记之间打开和结束<FooterTemplate>标记。 接下来,在开始标记和结束 <FooterTemplate> 标记之间添加自定义内容。 内容可以像纯文本或更复杂的 (在模板中嵌入其他控件(例如) )一样简单。

若要以编程方式访问模板中定义的控件,请先确定数据绑定控件中的哪个 TableCell 对象包含该控件。 接下来,使用 Controls 对象的集合 TableCell 来访问控件。 如果控件指定ID了属性,还可以使用FindControl对象的方法TableCell查找控件。

适用于

另请参阅