DetailsView.HeaderTemplate 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 DetailsView 控制項中,標頭資料列的使用者定義內容。
public:
virtual property System::Web::UI::ITemplate ^ HeaderTemplate { 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.WebControls.DetailsView))]
public virtual System.Web.UI.ITemplate HeaderTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.DetailsView))>]
member this.HeaderTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property HeaderTemplate As ITemplate
屬性值
ITemplate,包含標頭資料列的自訂內容。 預設值為 null
,表示這個屬性未設定。
- 屬性
範例
下列程式碼範例示範如何使用 HeaderTemplate 屬性來建立自訂標頭資料列。
<%@ 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">
void CustomerDetailView_ItemCreated(Object sender, EventArgs e)
{
// Get the header row.
DetailsViewRow headerRow = CustomerDetailView.HeaderRow;
// Get the Label control that displays the current page information
// from the header row.
Label pageNum = (Label)headerRow.FindControl("PageNumberLabel");
if(pageNum != null)
{
// Update the Label control with the current page number.
int page = CustomerDetailView.DataItemIndex + 1;
pageNum.Text = "Page " + page.ToString ();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView HeaderTemplate Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView HeaderTemplate Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
allowpaging="true"
onitemcreated="CustomerDetailView_ItemCreated"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
<pagersettings Mode="NextPreviousFirstLast"/>
<headertemplate>
<table width="100%">
<tr>
<td align="left">
<asp:Image id="LogoImage"
imageurl="~\images\Logo.jpg"
AlternateText="Our logo"
runat="server"/>
</td>
<td align="right" valign="bottom">
<asp:Label id="PageNumberLabel"
font-size="9"
forecolor="DodgerBlue"
runat="server"/>
</td>
</tr>
</table>
</headertemplate>
</asp:detailsview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</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">
Sub CustomerDetailView_ItemCreated(ByVal sender As Object, _
ByVal e As EventArgs)
' Get the header row.
Dim headerRow As DetailsViewRow = CustomerDetailView.HeaderRow
' Get the Label control that displays the current page information
' from the header row.
Dim pageNum As Label = _
CType(headerRow.FindControl("PageNumberLabel"), Label)
If Not pageNum Is Nothing Then
' Update the Label control with the current page number.
Dim page As Integer = CustomerDetailView.DataItemIndex + 1
pageNum.Text = "Page " & page.ToString()
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView HeaderTemplate Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView HeaderTemplate Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneraterows="true"
allowpaging="true"
onitemcreated="CustomerDetailView_ItemCreated"
runat="server">
<headerstyle backcolor="Navy"
forecolor="White"/>
<pagersettings Mode="NextPreviousFirstLast"/>
<headertemplate>
<table width="100%">
<tr>
<td align="left">
<asp:Image id="LogoImage"
imageurl="~\images\Logo.jpg"
AlternateText="Our logo"
runat="server"/>
</td>
<td align="right" valign="bottom">
<asp:Label id="PageNumberLabel"
font-size="9"
forecolor="DodgerBlue"
runat="server"/>
</td>
</tr>
</table>
</headertemplate>
</asp:detailsview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
備註
設定 或 HeaderTemplate 屬性時 HeaderText ,標頭資料列會顯示在控制項頂端 DetailsView 。 您可以使用 屬性,為標頭資料列 HeaderTemplate 定義自己的自訂使用者介面 (UI) 。 若要指定標頭資料列的自訂範本,請先在控制項的開頭和結束記號之間放置 <HeaderTemplate>
標記 DetailsView 。 然後,您可以在開頭和結尾 <HeaderTemplate>
標記之間列出範本的內容。 若要控制標頭資料列的樣式,請使用 HeaderStyle 屬性。 或者,您可以藉由設定 HeaderText 屬性而非此屬性,在標頭資料列中顯示文字。
注意
如果同時 HeaderText 設定 和 HeaderTemplate 屬性,則 HeaderTemplate 屬性會優先使用。