GridView.PagerTemplate Vlastnost

Definice

Získá nebo nastaví vlastní obsah pro řádek pager v ovládacím prvku GridView .

public:
 virtual property System::Web::UI::ITemplate ^ PagerTemplate { 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.GridViewRow))]
public virtual System.Web.UI.ITemplate PagerTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.GridViewRow))>]
member this.PagerTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property PagerTemplate As ITemplate

Hodnota vlastnosti

A ITemplate , který obsahuje vlastní obsah pro řádek pageru. Výchozí hodnota je null, což znamená, že tato vlastnost není nastavena.

Atributy

Příklady

Následující příklad ukazuje, jak vytvořit vlastní šablonu pageru, která uživateli umožňuje procházet ovládací prvek GridView pomocí ovládacího prvku DropDownList .


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

  protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
  {

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
    
    // Retrieve the PageDropDownList DropDownList from the bottom pager row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    // Set the PageIndex property to display that page selected by the user.
    CustomersGridView.PageIndex = pageList.SelectedIndex;

  }

  protected void CustomersGridView_DataBound(Object sender, EventArgs e)
  {

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
    
    // Retrieve the DropDownList and Label controls from the row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
    Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        
    if(pageList != null)
    {
        
      // Create the values for the DropDownList control based on 
      // the  total number of pages required to display the data
      // source.
      for(int i=0; i<CustomersGridView.PageCount; i++)
      {
            
        // Create a ListItem object to represent a page.
        int pageNumber = i + 1;
        ListItem item = new ListItem(pageNumber.ToString());         
            
        // If the ListItem object matches the currently selected
        // page, flag the ListItem object as being selected. Because
        // the DropDownList control is recreated each time the pager
        // row gets created, this will persist the selected item in
        // the DropDownList control.   
        if(i==CustomersGridView.PageIndex)
        {
          item.Selected = true;
        }
            
        // Add the ListItem object to the Items collection of the 
        // DropDownList.
        pageList.Items.Add(item);
                
      }
        
    }
        
    if(pageLabel != null)
    {
        
      // Calculate the current page number.
      int currentPage = CustomersGridView.PageIndex + 1;     
        
      // Update the Label control with the current page information.
      pageLabel.Text = "Page " + currentPage.ToString() +
        " of " + CustomersGridView.PageCount.ToString();
        
    }    
    
  }

</script>

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

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource"   
        autogeneratecolumns="true"
        allowpaging="true"
        ondatabound="CustomersGridView_DataBound"  
        runat="server">
              
        <pagerstyle forecolor="Blue"
          backcolor="LightBlue"/>
              
        <pagertemplate>
          
          <table width="100%">                    
            <tr>                        
              <td style="width:70%">
                          
                <asp:label id="MessageLabel"
                  forecolor="Blue"
                  text="Select a page:" 
                  runat="server"/>
                <asp:dropdownlist id="PageDropDownList"
                  autopostback="true"
                  onselectedindexchanged="PageDropDownList_SelectedIndexChanged" 
                  runat="server"/>
                      
              </td>   
                      
              <td style="width:70%; text-align:right">
                      
                <asp:label id="CurrentPageLabel"
                  forecolor="Blue"
                  runat="server"/>
                      
              </td>
                                            
            </tr>                    
          </table>
          
        </pagertemplate> 
          
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        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">

    Protected Sub PageDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    
        ' Retrieve the pager row.
        Dim pagerRow As GridViewRow = CustomersGridView.BottomPagerRow
    
        ' Retrieve the PageDropDownList DropDownList from the bottom pager row.
        Dim pageList As DropDownList = CType(pagerRow.Cells(0).FindControl("PageDropDownList"), DropDownList)
        
        ' Set the PageIndex property to display that page selected by the user.
        CustomersGridView.PageIndex = pageList.SelectedIndex
    
    End Sub
    
    Protected Sub CustomersGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    
        ' Retrieve the pager row.
        Dim pagerRow As GridViewRow = CustomersGridView.BottomPagerRow
    
        ' Retrieve the DropDownList and Label controls from the row.
        Dim pageList As DropDownList = CType(pagerRow.Cells(0).FindControl("PageDropDownList"), DropDownList)
        Dim pageLabel As Label = CType(pagerRow.Cells(0).FindControl("CurrentPageLabel"), Label)
        
        If Not pageList Is Nothing Then
        
            ' Create the values for the DropDownList control based on 
            ' the  total number of pages required to display the data
            ' source.
            Dim i As Integer
            
            For i = 0 To CustomersGridView.PageCount - 1
            
                ' Create a ListItem object to represent a page.
                Dim pageNumber As Integer = i + 1
                Dim item As ListItem = New ListItem(pageNumber.ToString())
            
                ' If the ListItem object matches the currently selected
                ' page, flag the ListItem object as being selected. Because
                ' the DropDownList control is recreated each time the pager
                ' row gets created, this will persist the selected item in
                ' the DropDownList control.   
                If i = CustomersGridView.PageIndex Then
          
                    item.Selected = True
                
                End If
            
                ' Add the ListItem object to the Items collection of the 
                ' DropDownList.
                pageList.Items.Add(item)
                
            Next i
        
        End If
        
        If Not pageLabel Is Nothing Then
        
            ' Calculate the current page number.
            Dim currentPage As Integer = CustomersGridView.PageIndex + 1
        
            ' Update the Label control with the current page information.
            pageLabel.Text = "Page " & currentPage.ToString() & _
                " of " & CustomersGridView.PageCount.ToString()
        
        End If
    
    End Sub

</script>

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

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource"   
        autogeneratecolumns="true"
        allowpaging="true"
        ondatabound="CustomersGridView_DataBound"  
        runat="server">
              
        <pagerstyle forecolor="Blue"
          backcolor="LightBlue"/>
              
        <pagertemplate>
          
          <table width="100%">                    
            <tr>                        
              <td style="width:70%">
                          
                <asp:label id="MessageLabel"
                  forecolor="Blue"
                  text="Select a page:" 
                  runat="server"/>
                <asp:dropdownlist id="PageDropDownList"
                  autopostback="true"
                  onselectedindexchanged="PageDropDownList_SelectedIndexChanged" 
                  runat="server"/>
                      
              </td>   
                      
              <td style="width:70%; text-align:right">
                      
                <asp:label id="CurrentPageLabel"
                  forecolor="Blue"
                  runat="server"/>
                      
              </td>
                                            
            </tr>                    
          </table>
          
        </pagertemplate> 
          
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Poznámky

Řádek stránkování se zobrazí v ovládacím GridView prvku, když je povolená funkce stránkování (když AllowPaging je vlastnost nastavena na true). Řádek pageru obsahuje ovládací prvky, které uživateli umožňují přejít na různé stránky ovládacího prvku. Místo použití integrovaného uživatelského rozhraní řádku pageru můžete definovat vlastní uživatelské rozhraní pomocí PagerTemplate vlastnosti .

Poznámka

PagerTemplate Když je vlastnost nastavená, přepíše předdefinované uživatelské rozhraní řádku pageru.

Pokud chcete zadat vlastní šablonu pro řádek pageru, umístěte <PagerTemplate> značky nejprve mezi počáteční a uzavírací značky GridView ovládacího prvku. Potom můžete zobrazit seznam obsahu šablony mezi otevírací a pravou <PagerTemplate> značkou. Pokud chcete řídit vzhled řádku pageru, použijte PagerStyle vlastnost .

Do šablony stránkovacího nástroje se obvykle přidávají ovládací prvky tlačítek, které provádějí operace stránkování. Ovládací GridView prvek provádí operaci stránkování při kliknutí na ovládací prvek tlačítka s jeho CommandName vlastností nastavenou na "Stránka". Vlastnost tlačítka CommandArgument určuje typ operace stránkování, která se má provést. Následující tabulka uvádí hodnoty argumentů příkazu podporované ovládacím GridView prvku.

CommandArgument Hodnotu Description
"Další" Přejde na další stránku.
"Předchozí" Přejde na předchozí stránku.
"První" Přejde na první stránku.
"Last" (Poslední) Přejde na poslední stránku.
Celočíselná hodnota Přejde na zadané číslo stránky.

Platí pro

Viz také