TemplateField.HeaderTemplate Vlastnost

Definice

Získá nebo nastaví šablonu pro zobrazení oddílu záhlaví objektu TemplateField .

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

Hodnota vlastnosti

ITemplate
  • ITemplateimplementovaný objekt, který obsahuje šablonu pro zobrazení oddílu TemplateField záhlaví ovládacího prvku vázaného na data. Výchozí hodnota je null, která označuje, že tato vlastnost není nastavena.
Atributy

Příklady

Následující příklad kódu ukazuje, jak pomocí HeaderTemplate vlastnosti vytvořit vlastní šablonu pro oddíl TemplateField záhlaví sloupce pole v ovládacím GridView prvku. Šablona zobrazí zaškrtávací políčko, které uživateli umožní zobrazit nebo skrýt řádky GridView ovládacího prvku.


<%@ 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 SelectAllCheckBox_CheckedChanged(Object sender, EventArgs e)
  {
  
    // Get the CheckBox control that indicates whether to show or 
    // hide the rows in the GridView control. The sender parameter
    // contains the control that raised the event.
    CheckBox showCheckBox = (CheckBox)sender;
    
    // Show or hide the rows of the GridView control based
    // on the check box value selected by the user.
    if (showCheckBox.Checked)
    {
      ShowRows (true);
    }
    else
    {
      ShowRows (false);
    }
    
  }
  
  void ShowRows(bool show)
  {
    // Iterate through the Rows collection of the GridView
    // control and show or hide the rows based on the value
    // of the show parameter.
    foreach(GridViewRow row in AuthorsGridView.Rows)
    {
      row.Visible = show;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField HeaderTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField HeaderTemplate Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a TemplateField column field that contains   -->
      <!-- a CheckBox control in the header section to show or -->
      <!-- hide the rows in the GridView control.              -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        width="250" 
        runat="server">
                
        <columns>
          <asp:templatefield>
            <headerstyle backcolor="Navy"
              forecolor="White"/>
            <itemtemplate>
              <%#Eval("au_fname")%>
              <%#Eval("au_lname")%>
            </itemtemplate>
            <headertemplate>
              <asp:checkbox id="ShowAllCheckBox"
                text="Show All"
                checked="True" 
                autopostback="true"
                oncheckedchanged="SelectAllCheckBox_CheckedChanged"  
                runat="server"/>
            </headertemplate>
          </asp:templatefield>                      
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;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">

  Sub SelectAllCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
  
    ' Get the CheckBox control that indicates whether to show or 
    ' hide the rows in the GridView control. The sender parameter
    ' contains the control that raised the event.
    Dim showCheckBox As CheckBox = CType(sender, CheckBox)
    
    ' Show or hide the rows of the GridView control based
    ' on the check box value selected by the user.
    If showCheckBox.Checked Then

      ShowRows(True)

    Else

      ShowRows(False)
    
    End If
    
  End Sub
  
  Sub ShowRows(ByVal show As Boolean)
    
    ' Iterate through the Rows collection of the GridView
    ' control and show or hide the rows based on the value
    ' of the show parameter.
    Dim row As GridViewRow
    For Each row In AuthorsGridView.Rows
    
      row.Visible = show
    
    Next
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TemplateField HeaderTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField HeaderTemplate Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a TemplateField column field that contains   -->
      <!-- a CheckBox control in the header section to show or -->
      <!-- hide the rows in the GridView control.              -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        width="250" 
        runat="server">
                
        <columns>
          <asp:templatefield>
            <headerstyle backcolor="Navy"
              forecolor="White"/>
            <itemtemplate>
              <%#Eval("au_fname")%>
              <%#Eval("au_lname")%>
            </itemtemplate>
            <headertemplate>
              <asp:checkbox id="ShowAllCheckBox"
                text="Show All"
                checked="True" 
                autopostback="true"
                oncheckedchanged="SelectAllCheckBox_CheckedChanged"  
                runat="server"/>
            </headertemplate>
          </asp:templatefield>                      
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Poznámky

HeaderTemplate Vlastnost slouží k určení vlastního obsahu zobrazeného pro oddíl záhlaví objektuTemplateField. Definujte obsah vytvořením šablony, která určuje způsob vykreslení oddílu záhlaví.

Pokud chcete zadat šablonu, nejprve umístěte levou a pravou <HeaderTemplate> značku mezi levou a pravou značkou <TemplateField> prvku. Dále přidejte vlastní obsah mezi levou a pravou <HeaderTemplate> značkou. Obsah může být stejně jednoduchý jako prostý text nebo složitější (vložení dalších ovládacích prvků do šablony, například).

Pokud chcete získat programový přístup k ovládacímu prvku definovanému v šabloně, nejprve určete, který TableCell objekt v ovládacím prvku vázaném na data obsahuje ovládací prvek. Dále použijte Controls kolekci objektu TableCell pro přístup k ovládacímu prvku. Můžete také použít FindControl metodu TableCell objektu k vyhledání ovládacího prvku, pokud má ovládací prvek zadanou ID vlastnost.

Platí pro

Viz také