DetailsView.HeaderRow Właściwość

Definicja

DetailsViewRow Pobiera obiekt reprezentujący wiersz nagłówka w kontrolceDetailsView.

public:
 virtual property System::Web::UI::WebControls::DetailsViewRow ^ HeaderRow { System::Web::UI::WebControls::DetailsViewRow ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.DetailsViewRow HeaderRow { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HeaderRow : System.Web.UI.WebControls.DetailsViewRow
Public Overridable ReadOnly Property HeaderRow As DetailsViewRow

Wartość właściwości

Element DetailsViewRow reprezentujący wiersz nagłówka w kontrolce DetailsView .

Atrybuty

Przykłady

Poniższy przykład kodu pokazuje, jak używać HeaderRow właściwości w celu uzyskania dostępu do wiersza nagłówka DetailsView kontrolki ItemCreated podczas zdarzenia. Kontrolka Label w niestandardowym wierszu nagłówka jest następnie aktualizowana przy użyciu bieżącego numeru strony.


<%@ 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>

Uwagi

Wiersz nagłówka jest wyświetlany w górnej części kontrolki DetailsView po ustawieniu HeaderText właściwości lub HeaderTemplate . Użyj właściwości , HeaderRow aby programowo uzyskać dostęp do DetailsViewRow obiektu reprezentującego wiersz nagłówka.

Uwaga

Właściwość HeaderRow jest dostępna tylko po utworzeniu DetailsView wiersza nagłówka w zdarzeniu ItemCreated .

Ta właściwość jest często używana, gdy trzeba programowo manipulować wierszem nagłówka, na przykład podczas dodawania zawartości niestandardowej. Wszelkie modyfikacje HeaderRow właściwości należy wykonać po renderowaniu DetailsView kontrolki. W przeciwnym razie kontrolka DetailsView zastąpi wszelkie zmiany.

Dotyczy

Zobacz też