DataPager Class

Definition

Provides paging functionality for data-bound controls that implement the IPageableItemContainer interface, such as the ListView control.

[System.Drawing.ToolboxBitmap(typeof(System.Web.UI.WebControls.DataPager), "DataPager.bmp")]
[System.Web.UI.Themeable(true)]
public class DataPager : System.Web.UI.Control, System.Web.UI.IAttributeAccessor, System.Web.UI.INamingContainer, System.Web.UI.WebControls.ICompositeControlDesignerAccessor
Inheritance
DataPager
Attributes
Implements

Examples

The following example shows how to add paging functionality to a ListView control. This example contains two DataPager controls that are used to page through data of the same ListView control.

<%@ Page language="C#" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>DataPager Example</title>
    <style type="text/css">        
      th
      {
        background-color:#eef4fa;
        border-top:solid 1px #9dbbcc;
        border-bottom:solid 1px #9dbbcc;
      }
      .itemSeparator { border-right: 1px solid #ccc }
      .groupSeparator
      {
        height: 1px;
        background-color: #cccccc;
      }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataPager Example</h3>
      
      <!-- The first DataPager control. -->
      <asp:DataPager runat="server" ID="BeforeListDataPager"
        PagedControlID="ProductsListView" 
        PageSize="18">
        <Fields>
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowFirstPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            FirstPageImageUrl="~/images/first.gif" />
          <asp:NumericPagerField ButtonCount="10" />
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowLastPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            LastPageImageUrl="~/images/last.gif" />
        </Fields>
      </asp:DataPager>

      <asp:ListView ID="ProductsListView" 
        DataSourceID="ProductsDataSource" 
        GroupItemCount="3"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" id="tbl1" runat="server">
            <tr>
              <th colspan="5">PRODUCTS LIST</th>
            </tr>
            <tr runat="server" id="groupPlaceholder"></tr>
          </table>
        </LayoutTemplate>
        <GroupTemplate>
          <tr runat="server" id="tr1">
            <td runat="server" id="itemPlaceholder"></td>
          </tr>
        </GroupTemplate>
        <GroupSeparatorTemplate>
          <tr runat="server">
            <td colspan="5">
	            <div class="groupSeparator"><hr></div>
	          </td>
          </tr>
        </GroupSeparatorTemplate>
        <ItemTemplate>
          <td align="center" runat="server">
            <asp:HyperLink ID="ProductLink" runat="server" 
              Text='<%# Eval("Name") %>' 
              NavigateUrl='<%# "ProductDetails.aspx?productID=" + Eval("ProductID") %>' /><br />
            <asp:Image ID="ProductImage" runat="server"
              ImageUrl='<%#"~/images/thumbnails/" + Eval("ThumbnailPhotoFileName") %>' /><br />
            <b>Price:</b> <%# Eval("ListPrice", "{0:c}")%> <br />
          </td>
        </ItemTemplate>
        <ItemSeparatorTemplate>
          <td class="itemSeparator" runat="server">&nbsp;</td>
        </ItemSeparatorTemplate>
      </asp:ListView>

      <!-- The second DataPager control. -->
      <asp:DataPager runat="server" ID="AfterListDataPager"
        PagedControlID="ProductsListView" 
        PageSize="18">
        <Fields>
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowFirstPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            FirstPageImageUrl="~/images/first.gif" />
          <asp:NumericPagerField ButtonCount="10" />
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowLastPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            LastPageImageUrl="~/images/last.gif" />
        </Fields>
      </asp:DataPager>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ProductsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"            	        
        SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice, 
          PF.ThumbnailPhotoFileName
          FROM Production.Product AS P 
          INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID 
          INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID">
      </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">
    
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>DataPager Example</title>
    <style type="text/css">        
      th
      {
        background-color:#eef4fa;
        border-top:solid 1px #9dbbcc;
        border-bottom:solid 1px #9dbbcc;
      }
      .itemSeparator { border-right: 1px solid #ccc }
      .groupSeparator
      {
        height: 1px;
        background-color: #cccccc;
      }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DataPager Example</h3>
      
      <!-- The first DataPager control. -->
      <asp:DataPager runat="server" ID="BeforeListDataPager"
        PagedControlID="ProductsListView" 
        PageSize="18">
        <Fields>
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowFirstPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            FirstPageImageUrl="~/images/first.gif" />
          <asp:NumericPagerField ButtonCount="10" />
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowLastPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            LastPageImageUrl="~/images/last.gif" />
        </Fields>
      </asp:DataPager>

      <asp:ListView ID="ProductsListView" 
        DataSourceID="ProductsDataSource" 
        GroupItemCount="3"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" id="tbl1" runat="server">
            <tr>
              <th colspan="5">PRODUCTS LIST</th>
            </tr>
            <tr runat="server" id="groupPlaceholder"></tr>
          </table>
        </LayoutTemplate>
        <GroupTemplate>
          <tr runat="server" id="tr1">
            <td runat="server" id="itemPlaceholder"></td>
          </tr>
        </GroupTemplate>
        <GroupSeparatorTemplate>
          <tr runat="server">
            <td colspan="5">
	            <div class="groupSeparator"><hr></div>
	          </td>
          </tr>
        </GroupSeparatorTemplate>
        <ItemTemplate>
          <td align="center" runat="server">
            <asp:HyperLink ID="ProductLink" runat="server" 
              Text='<%# Eval("Name") %>' 
              NavigateUrl='<%# "ProductDetails.aspx?productID=" & Eval("ProductID") %>' /><br />
            <asp:Image ID="ProductImage" runat="server"
              ImageUrl='<%#"~/images/thumbnails/" & Eval("ThumbnailPhotoFileName") %>' /><br />
            <b>Price:</b> <%# Eval("ListPrice", "{0:c}")%> <br />
          </td>
        </ItemTemplate>
        <ItemSeparatorTemplate>
          <td class="itemSeparator" runat="server">&nbsp;</td>
        </ItemSeparatorTemplate>
      </asp:ListView>

      <!-- The second DataPager control. -->
      <asp:DataPager runat="server" ID="AfterListDataPager"
        PagedControlID="ProductsListView" 
        PageSize="18">
        <Fields>
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowFirstPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            FirstPageImageUrl="~/images/first.gif" />
          <asp:NumericPagerField ButtonCount="10" />
          <asp:NextPreviousPagerField ButtonType="Image"
            ShowLastPageButton="true"
            ShowNextPageButton="false"
            ShowPreviousPageButton="false"
            LastPageImageUrl="~/images/last.gif" />
        </Fields>
      </asp:DataPager>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="ProductsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"            	        
        SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice, 
          PF.ThumbnailPhotoFileName
          FROM Production.Product AS P 
          INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID 
          INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

Remarks

In this topic:

Introduction

The DataPager class is used to page data and to display navigation controls for data-bound controls that implement the IPageableItemContainer interface. (An example of a control that implements the interface is the ListView control.)

You can associate the DataPager control with the data-bound control by using the PagedControlID property. Alternatively, you can put the DataPager control inside the data-bound control hierarchy. For example, in the ListView control, you can put the DataPager control inside the ListView.LayoutTemplate template.

You can customize the number of items that are displayed for each page of data by changing the PageSize property. You can also change the way a page is submitted to the server by setting the QueryStringField property.

Pager Fields

In order for the DataPager control to display navigation controls, you must add pager fields to the control. The pager fields derive from the DataPagerField class. The following table lists the pager field types that you can use.

Pager field type Description
NextPreviousPagerField Enables users to navigate through pages one page at a time, or to jump to the first or last page.
NumericPagerField Enables users to select a page by page number.
TemplatePagerField Enables you to create a custom paging UI.

To declaratively add pager fields to the DataPager control, add a Fields element to the DataPager control. You can then add the pager fields to the Fields element. The pager fields are added to the Fields collection in the order that they appear in the Fields element. The Fields collection enables you to programmatically manage the pager fields in the DataPager control.

Page Properties

The following table lists read-only properties of the DataPager control that specify characteristics of the page of data. These properties are usually used for binding expressions in the TemplatePagerField object.

Property Description
MaximumRows The maximum number of records that are displayed for each page of data.
StartRowIndex The index of the first record that is displayed on a page of data.
TotalRowCount The total number of records that are available in the underlying data source.

Accessibility

The default markup that is rendered default for this control might not comply with accessibility standards, such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

Declarative Syntax

<asp:DataPager  
    EnableTheming="True|False"  
    EnableViewState="True|False"  
    ID="string"  
    OnDataBinding="DataBinding event handler"  
    OnDisposed="Disposed event handler"  
    OnInit="Init event handler"  
    OnLoad="Load event handler"  
    OnPreRender="PreRender event handler"  
    OnUnload="Unload event handler"  
    PagedControlID="string"  
    PageSize="integer"  
    runat="server"  
    SkinID="string"  
    Visible="True|False"  
>  
        <Fields>  
            <asp:NextPreviousPagerField  
                ButtonCssClass="string"  
                ButtonType="Button|Image|Link"  
                FirstPageImageUrl="string"  
                FirstPageText="string"  
                LastPageImageUrl="string"  
                LastPageText="string"  
                NextPageImageUrl="string"  
                NextPageText="string"  
                PreviousPageImageUrl="string"  
                PreviousPageText="string"  
                RenderDisabledButtonsAsLabels="True|False"  
                RenderNonBreakingSpacesBetweenControls="True|False"  
                ShowFirstPageButton="True|False"  
                ShowLastPageButton="True|False"  
                ShowNextPageButton="True|False"  
                ShowPreviousPageButton="True|False"  
                Visible="True|False"  
            />  
            <asp:NumericPagerField  
                ButtonCount="integer"  
                ButtonType="Button|Image|Link"  
                CurrentPageLabelCssClass="string"  
                NextPageImageUrl="string"  
                NextPageText="string"  
                NextPreviousButtonCssClass="string"  
                NumericButtonCssClass="string"                 PreviousPageImageUrl="string"  
                PreviousPageText="string"  
                RenderNonBreakingSpacesBetweenControls="True|False"  
                Visible="True|False"  
            />  
            <asp:TemplatePagerField  
                OnPagerCommand="PagerCommand event handler"  
                Visible="True|False"  
            />  
        </Fields>  
</asp:DataPager>  

Constructors

DataPager()

Initializes a new instance of the DataPager class.

Properties

Adapter Inherited from Control
AppRelativeTemplateSourceDirectory Inherited from Control
Attributes

Gets a collection of custom attribute name/value pairs for the DataPager control.

BindingContainer Inherited from Control
ChildControlsCreated Inherited from Control
ClientID Inherited from Control
ClientIDMode Inherited from Control
ClientIDSeparator Inherited from Control
Context Inherited from Control
Controls

Gets a ControlCollection object that represents the child controls for the DataPager control in the UI hierarchy.

DataItemContainer Inherited from Control
DataKeysContainer Inherited from Control
DesignMode Inherited from Control
EnableTheming Inherited from Control
EnableViewState Inherited from Control
Events Inherited from Control
Fields

Gets a collection of DataPagerField objects that represent the pager fields that are specified in a DataPager control.

HasChildViewState Inherited from Control
IControlBuilderAccessor.ControlBuilder Inherited from Control
IControlDesignerAccessor.UserData Inherited from Control
ID Inherited from Control
IDataBindingsAccessor.DataBindings Inherited from Control
IDataBindingsAccessor.HasDataBindings Inherited from Control
IExpressionsAccessor.Expressions Inherited from Control
IExpressionsAccessor.HasExpressions Inherited from Control
IdSeparator Inherited from Control
IsChildControlStateCleared Inherited from Control
IsTrackingViewState Inherited from Control
IsViewStateEnabled Inherited from Control
LoadViewStateByID Inherited from Control
MaximumRows

Gets the maximum number of records that are displayed for each page of data.

NamingContainer Inherited from Control
Page Inherited from Control
PageSize

Gets or sets the number of records that are displayed for each page of data.

PagedControlID

Gets or sets the ID of the control that contains the data that will be paged by the DataPager control.

Parent Inherited from Control
QueryStringField

Gets or sets the name of the query string field.

RenderingCompatibility Inherited from Control
Site Inherited from Control
SkinID Inherited from Control
StartRowIndex

Gets the index of the first record that is displayed on a page of data .

TagKey

Gets the HTML element that is used to render the DataPager control.

TemplateControl Inherited from Control
TemplateSourceDirectory Inherited from Control
TotalRowCount

Gets the total number of records that are retrieved by the underlying data source object that is referenced by the associated data-bound control.

UniqueID Inherited from Control
ValidateRequestMode Inherited from Control
ViewState Inherited from Control
ViewStateIgnoresCase Inherited from Control
ViewStateMode Inherited from Control
Visible Inherited from Control

Methods

AddAttributesToRender(HtmlTextWriter)

Adds HTML attributes and styles to be rendered in the browser to the specified HtmlTextWriter object.

AddParsedSubObject(Object) Inherited from Control
AddedControl(Control, Int32) Inherited from Control
ApplyStyleSheetSkin(Page) Inherited from Control
BeginRenderTracing(TextWriter, Object) Inherited from Control
BuildProfileTree(String, Boolean) Inherited from Control
ClearCachedClientID() Inherited from Control
ClearChildControlState() Inherited from Control
ClearChildState() Inherited from Control
ClearChildViewState() Inherited from Control
ClearEffectiveClientIDMode() Inherited from Control
ConnectToEvents(IPageableItemContainer)

Connects events to event handler methods in the DataPager control.

CreateChildControls() Inherited from Control
CreateControlCollection() Inherited from Control
CreatePagerFields()

Creates the DataPagerField objects that are contained by the Fields property of the DataPager control.

DataBind()

Binds the DataPager control and all its child controls to a data source.

DataBind(Boolean) Inherited from Control
DataBindChildren() Inherited from Control
Dispose() Inherited from Control
EndRenderTracing(TextWriter, Object) Inherited from Control
EnsureChildControls() Inherited from Control
EnsureID() Inherited from Control
Equals(Object) Inherited from Object
Equals(Object, Object) Inherited from Object
FindControl(String) Inherited from Control
FindControl(String, Int32) Inherited from Control
FindPageableItemContainer()

Retrieves the data-bound control that is associated with the DataPager control.

Focus() Inherited from Control
GetDesignModeState() Inherited from Control
GetHashCode() Inherited from Object
GetRouteUrl(Object) Inherited from Control
GetRouteUrl(RouteValueDictionary) Inherited from Control
GetRouteUrl(String, Object) Inherited from Control
GetRouteUrl(String, RouteValueDictionary) Inherited from Control
GetType() Inherited from Object
GetUniqueIDRelativeTo(Control) Inherited from Control
HasControls() Inherited from Control
HasEvents() Inherited from Control
IControlDesignerAccessor.GetDesignModeState() Inherited from Control
IControlDesignerAccessor.SetDesignModeState(IDictionary) Inherited from Control
IControlDesignerAccessor.SetOwnerControl(Control) Inherited from Control
IParserAccessor.AddParsedSubObject(Object) Inherited from Control
IsLiteralContent() Inherited from Control
LoadControlState(Object)

Loads the state information for the properties in the DataPager control that must be persisted between postbacks, even when the EnableViewState property is set to false.

LoadViewState(Object)

Restores view-state information for the DataPager control that was saved by the SaveViewState() method during a previous page request.

MapPathSecure(String) Inherited from Control
MemberwiseClone() Inherited from Object
OnBubbleEvent(Object, EventArgs)

Determines whether an event for the DataPager control is passed up the page's user interface (UI) server control hierarchy

OnDataBinding(EventArgs) Inherited from Control
OnInit(EventArgs)

Raises the Init event.

OnLoad(EventArgs)

Raises the Load event.

OnPreRender(EventArgs) Inherited from Control
OnTotalRowCountAvailable(Object, PageEventArgs)

Handles the TotalRowCountAvailable event.

OnUnload(EventArgs) Inherited from Control
OpenFile(String) Inherited from Control
RaiseBubbleEvent(Object, EventArgs) Inherited from Control
RecreateChildControls()

Creates child controls of the DataPager control at design time.

ReferenceEquals(Object, Object) Inherited from Object
RemovedControl(Control) Inherited from Control
Render(HtmlTextWriter)

Sends the DataPager control content to the specified HtmlTextWriter object, which writes the content to be rendered to the browser.

RenderBeginTag(HtmlTextWriter)

Renders the HTML opening tag of the DataPager control to the specified writer.

RenderChildren(HtmlTextWriter) Inherited from Control
RenderContents(HtmlTextWriter)

Renders the contents of the DataPager control to the specified writer.

RenderControl(HtmlTextWriter) Inherited from Control
RenderControl(HtmlTextWriter, ControlAdapter) Inherited from Control
ResolveAdapter() Inherited from Control
ResolveClientUrl(String) Inherited from Control
ResolveUrl(String) Inherited from Control
SaveControlState()

Saves the state of the properties in the DataPager control that must be persisted between postbacks, even when the EnableViewState property is set to false.

SaveViewState()

Saves any changes to an DataPager control's view-state that have occurred since the time the page was posted back to the server.

SetDesignModeState(IDictionary) Inherited from Control
SetPageProperties(Int32, Int32, Boolean)

Sets the page-related properties in the DataPager control.

SetRenderMethodDelegate(RenderMethod) Inherited from Control
SetTraceData(Object, Object) Inherited from Control
SetTraceData(Object, Object, Object) Inherited from Control
ToString() Inherited from Object
TrackViewState()

Tracks view-state changes to the DataPager control so that they can be stored in the control's StateBag object. This object can be accessed through the ViewState property.

Explicit Interface Implementations

IAttributeAccessor.GetAttribute(String)

Retrieves the attribute value with the specified name from the DataPager control.

IAttributeAccessor.SetAttribute(String, String)

Sets an attribute of the DataPager control with the specified name and value.

ICompositeControlDesignerAccessor.RecreateChildControls()

Creates the child controls of the DataPager control at design time.

Events

DataBinding Inherited from Control
Disposed Inherited from Control
Init Inherited from Control
Load Inherited from Control
PreRender Inherited from Control
Unload Inherited from Control

Extension Methods

FindDataSourceControl(Control)

Returns the data source that is associated with the data control for the specified control.

FindFieldTemplate(Control, String)

Returns the field template for the specified column in the specified control's naming container.

FindMetaTable(Control)

Returns the metatable object for the containing data control.

GetDefaultValues(INamingContainer)

Gets the collection of the default values for the specified data control.

GetMetaTable(INamingContainer)

Gets the table metadata for the specified data control.

SetMetaTable(INamingContainer, MetaTable)

Sets the table metadata for the specified data control.

SetMetaTable(INamingContainer, MetaTable, IDictionary<String,Object>)

Sets the table metadata and default value mapping for the specified data control.

SetMetaTable(INamingContainer, MetaTable, Object)

Sets the table metadata and default value mapping for the specified data control.

TryGetMetaTable(INamingContainer, MetaTable)

Determines whether table metadata is available.

EnableDynamicData(INamingContainer, Type)

Enables Dynamic Data behavior for the specified data control.

EnableDynamicData(INamingContainer, Type, IDictionary<String,Object>)

Enables Dynamic Data behavior for the specified data control.

EnableDynamicData(INamingContainer, Type, Object)

Enables Dynamic Data behavior for the specified data control.

Applies to

See Also