Export (0) Print
Expand All

GridView.TopPagerRow Property

Gets a GridViewRow object that represents the top pager row in a GridView control.

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

[BrowsableAttribute(false)]
public virtual GridViewRow TopPagerRow { get; }

Property Value

Type: System.Web.UI.WebControls.GridViewRow
A GridViewRow that represents the top pager row in the control.

When paging is enabled (by setting the AllowPaging property to true), an additional row called the pager row is automatically displayed in the GridView control. The pager row contains controls that allow the user to navigate to the other pages and can be displayed at the top, the bottom, or both the top and bottom of the control. Use the TopPagerRow property to programmatically access the GridViewRow object that represents the top pager row in a GridView control.

NoteNote:

The TopPagerRow property is available only after the GridView control creates the top pager row in the RowCreated event.

This property is commonly used when you need to programmatically manipulate the top pager row, for example when adding custom content. Any modification to the TopPagerRow property must be performed after the GridView control has been rendered; otherwise, the GridView control will overwrite any changes.

The following example demonstrates how to use the TopPagerRow property to access the top pager row in a GridView control. The TopPagerRow property is used to retrieve a DropDownList control from the pager row.

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

    // Retrieve the pager row.
    GridViewRow pagerRow = CustomersGridView.TopPagerRow;

    // Retrieve the PageDropDownList DropDownList from the 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;

  }

  void CustomersGridView_DataBound(Object sender, EventArgs e)
  {

    // Retrieve the PagerRow.
    GridViewRow pagerRow = CustomersGridView.TopPagerRow;

    // 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 TopPagerRow Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView TopPagerRow Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource"   
        autogeneratecolumns="true"
        allowpaging="true"
        ondatabound="CustomersGridView_DataBound"  
        runat="server">

        <pagerstyle forecolor="Blue"
          backcolor="LightBlue"/>

        <pagersettings position="Top"/>

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

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft