TemplateField.FooterTemplate 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定顯示 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 實作的物件,包含顯示 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 顯示的自訂內容。 藉由建立範本來定義內容,以指定頁尾區段的呈現方式。
若要指定範本,請先在專案的開頭和結束記號之間放置開頭和結尾 <FooterTemplate>
標記 <TemplateField>
。 接下來,在開頭和結尾 <FooterTemplate>
標記之間新增自訂內容。 內容可以像純文字一樣簡單,或更複雜的 (在範本中內嵌其他控制項,例如) 。
若要以程式設計方式存取範本中定義的控制項,請先判斷資料繫結控制項中的哪個 TableCell 物件包含 控制項。 接下來,使用 Controls 物件的集合 TableCell 來存取 控制項。 如果控制項已 ID 指定屬性,您也可以使用 FindControl 物件的 方法來 TableCell 尋找控制項。