GridView..::.PagerTemplate Property
This page is specific to:.NET Framework Version:2.03.03.54.0
.NET Framework Class Library
GridView..::.PagerTemplate Property

Gets or sets the custom content for the pager row in a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Syntax

'Usage

Dim instance As GridView
Dim value As ITemplate

value = instance.PagerTemplate

instance.PagerTemplate = value

'Declaration

<BrowsableAttribute(False)> _
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> _
<TemplateContainerAttribute(GetType(GridViewRow))> _
Public Overridable Property PagerTemplate As ITemplate
<asp:GridView>
    <PagerTemplate>ITemplate</PagerTemplate>
</asp:GridView>

Property Value

Type: System.Web.UI..::.ITemplate
A System.Web.UI..::.ITemplate that contains the custom content for the pager row. The default value is null, which indicates that this property is not set.
Remarks

A pager row is displayed in a GridView control when the paging feature is enabled (when the AllowPaging property is set to true). The pager row contains the controls that allow the user to navigate to the different pages in the control. Instead of using the built-in pager row user interface (UI), you can define your own UI by using the PagerTemplate property.

NoteNote:

When the PagerTemplate property is set, it overrides the built-in pager row UI.

To specify a custom template for the pager row, first place <PagerTemplate> tags between the opening and closing tags of the GridView control. You can then list the contents of the template between the opening and closing <PagerTemplate> tags. To control the appearance of the pager row, use the PagerStyle property.

Typically, button controls are added to the pager template to perform the paging operations. The GridView control performs a paging operation when a button control with its CommandName property set to "Page" is clicked. The button's CommandArgument property determines the type of paging operation to perform. The following table lists the command argument values supported by the GridView control.

CommandArgument value

Description

"Next"

Navigates to the next page.

"Prev"

Navigates to the previous page.

"First"

Navigates to the first page.

"Last"

Navigates to the last page.

Integer value

Navigates to the specified page number.

Examples

The following example demonstrates how to create a custom pager template that allows the user to navigate through a GridView control using a DropDownList control.

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



Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View