FormView.HeaderTemplate 屬性

定義

取得或設定 FormView 控制項中,標頭資料列的使用者定義內容。

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.FormView))]
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.FormView))>]
member this.HeaderTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property HeaderTemplate As ITemplate

屬性值

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 EmployeeFormView_ItemCreated(Object sender, EventArgs e)
  {

    // Use the HeaderRow property to retrieve the header row.
    FormViewRow header = EmployeeFormView.HeaderRow;

    // Retrieve the HeaderLabel Label control from the header row. 
    Label headerLabel = (Label)header.FindControl("HeaderLabel");

    if(headerLabel != null)
    {
      // Display the current page number.
      int currentPage = EmployeeFormView.PageIndex + 1;
      headerLabel.Text = "Page " + currentPage.ToString();
    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView HeaderTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView HeaderTemplate Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true" 
        datakeynames="EmployeeID"
        onitemcreated="EmployeeFormView_ItemCreated"
        runat="server">
        
        <headertemplate>
          <table>
            <tr>
              <td>
                <asp:image id="LogoImage"
                  imageurl="~/Images/LogoImage.jpg"
                  alternatetext="Our Logo"
                  runat="server"/>
              </td>
              <td>
                <asp:label id="HeaderLabel"
                  runat="server"/>
              </td>
            </tr>
          </table>
        </headertemplate>
        
        <headerstyle horizontalalign="Center"
          forecolor="White"
          backcolor="LightBlue"/>
           
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
        <pagersettings position="Bottom"
          mode="NextPrevious"/> 
                  
      </asp:formview>
          
      <!-- 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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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 EmployeeFormView_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
  
    ' Use the HeaderRow property to retrieve the header row.
    Dim header As FormViewRow = EmployeeFormView.HeaderRow

    ' Retrieve the HeaderLabel Label control from the header row. 
    Dim headerLabel As Label = CType(header.FindControl("HeaderLabel"), Label)

    If headerLabel IsNot Nothing Then
    
      ' Display the current page number.
      Dim currentPage As Integer = EmployeeFormView.PageIndex + 1
      headerLabel.Text = "Page " & currentPage.ToString()
    
    End If

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView HeaderTemplate Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>FormView HeaderTemplate Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true" 
        datakeynames="EmployeeID"
        onitemcreated="EmployeeFormView_ItemCreated"
        runat="server">
        
        <headertemplate>
          <table>
            <tr>
              <td>
                <asp:image id="LogoImage"
                  imageurl="~/Images/LogoImage.jpg"
                  alternatetext="Our Logo"
                  runat="server"/>
              </td>
              <td>
                <asp:label id="HeaderLabel"
                  runat="server"/>
              </td>
            </tr>
          </table>
        </headertemplate>
        
        <headerstyle horizontalalign="Center"
          forecolor="White"
          backcolor="LightBlue"/>
           
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
          </table>
        
        </itemtemplate>
          
        <pagersettings position="Bottom"
          mode="NextPrevious"/> 
                  
      </asp:formview>
          
      <!-- 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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

備註

設定 或 HeaderTemplate 屬性時 HeaderText ,標頭資料列會顯示在控制項頂端 FormView 。 您可以使用 屬性,為標頭資料列 HeaderTemplate 定義自己的自訂使用者介面 (UI) 。 若要指定標頭資料列的自訂範本,請先在控制項的開頭和結束記號之間放置 <HeaderTemplate> 標記 FormView 。 然後,您可以在開頭和結尾 <HeaderTemplate> 標記之間列出範本的內容。 若要控制標頭資料列的樣式,請使用 HeaderStyle 屬性。 或者,您可以藉由設定 HeaderText 屬性而非此屬性,在標頭資料列中顯示文字。

注意

如果同時 HeaderText 設定 和 HeaderTemplate 屬性,則 HeaderTemplate 屬性會優先使用。

適用於

另請參閱