SIGN UP MEMBER LOGIN:    
ARTICLE

GridView with DataPager in ASP.NET 3.5

Posted by Nipun Tomar Articles | ASP.NET Controls in C# August 01, 2008
To page through data in a control that implements the IPageableItemContainer interface, DataPager control can be used. GridView has its own paging and does not implement IPageableItemContainer interface.
Reader Level:
Download Files:
 

To page through data in a control that implements the IPageableItemContainer interface, DataPager control can be used. GridView has its own paging and does not implement IPageableItemContainer interface. ListView is the only control that works with DataPager.

The DataPager control supports built-in paging user interface (UI). NumericPagerField object enables users to select a page of data by page number. NextPreviousPagerField object enables users to move through pages of data one page at a time, or to jump to the first or last page of data. The size of the pages of data is set by using the PageSize property of the DataPager control. One or more pager field objects can be used in a single DataPager control. Custom paging UI can be created by using the TemplatePagerField object. In the TemplatePagerField template, the DataPager control is referenced by using the Container property which provides access to the properties of the DataPager control. These properties include the starting row index, the page size, and the total number of rows currently bound to the control.

So let's begin extending the GridView with the IPageableItemContainer interface.

Define a new class as below
/// <summary>

/// DataPagerGridView is a custom control that implements GrieView and IPageableItemContainer

/// </summary>

public class DataPagerGridView : GridView, IPageableItemContainer

{
}

MaximumRows will be equal to the PageSize property
/// <summary>

/// IPageableItemContainer's MaximumRows  = PageSize property

/// </summary>

int IPageableItemContainer.MaximumRows

{

    get { return this.PageSize; }

}

StartRowIndex can be calculated from the PageSize and PageIndex properties
/// <summary>

/// IPageableItemContainer's StartRowIndex = PageSize * PageIndex properties

/// </summary>

int IPageableItemContainer.StartRowIndex

{

    get { return this.PageSize * this.PageIndex; }

}

The SetPageProperties method is called by the DataPager control every time that the control must update the page-related properties. When implemented by a class, the SetPageProperties method must internally update the MaximumRows and StartRowIndex properties by using the values of the maximumRows and startRowIndex parameters. So set the Grid with appropriate parameters and bind to right chunk of data.
/// <summary>

/// Set the control with appropriate parameters and bind to right chunk of data.

/// </summary>

/// <param name="startRowIndex"></param>

/// <param name="maximumRows"></param>

/// <param name="databind"></param>

void IPageableItemContainer.SetPageProperties(int startRowIndex, int maximumRows, bool databind)

{

    int newPageIndex = (startRowIndex / maximumRows);

    this.PageSize = maximumRows;

    if (this.PageIndex != newPageIndex)

    {

        bool isCanceled = false;

        if (databind)

        {

            //  create the event arguments and raise the event

            GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex);

            this.OnPageIndexChanging(args);

            isCanceled = args.Cancel;

            newPageIndex = args.NewPageIndex;

        }

        //  if the event wasn't cancelled change the paging values

        if (!isCanceled)

        {

            this.PageIndex = newPageIndex;

            if (databind)

                this.OnPageIndexChanged(EventArgs.Empty);

        }

        if (databind)

            this.RequiresDataBinding = true;

    }

}

For the DataPager to render the correct number of page buttons and enable/disable them, it needs to know the total number of rows and the Page Size. But this information is with the GridView and not known to the DataPager.As the GridView control inherits from CompositeDataboundControl which contains a variant of the CreateChildControls method that also takes in a property indicating whether the control is being bound to data or simply re-rendered. The GridView uses this method to bind to its data source and here we can place a trigger for the TotalRowCountAvailable event to be raised. Call base control's CreateChildControls method and determine the number of rows in the source then fire off the event with the derived data and then we return the original result.
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)

{

    int rows = base.CreateChildControls(dataSource, dataBinding);

    //  if the paging feature is enabled, determine the total number of rows in the datasource

    if (this.AllowPaging)

    {

    //  if we are databinding, use the number of rows that were created, otherwise cast the datasource to an Collection and use that as the count

    int totalRowCount = dataBinding ? rows : ((ICollection)dataSource).Count;

    //  raise the row count available event

    IPageableItemContainer pageableItemContainer = this as IPageableItemContainer;

    this.OnTotalRowCountAvailable(new PageEventArgs(pageableItemContainer.StartRowIndex,pageableItemContainer.MaximumRows,totalRowCount));

    //  make sure the top and bottom pager rows are not visible

    if (this.TopPagerRow != null)

        this.TopPagerRow.Visible = false;

    if (this.BottomPagerRow != null)

        this.BottomPagerRow.Visible = false;

    }

    return rows
}

 That's all you are done, put the control on aspx page and use DataPager with GridView Contol.

 

 

Login to add your contents and source code to this article
share this article :
post comment
 

When I try to go to the next page, it doesn't show any data. page 1 is fine. Should I write another bind Thanks Sun

Posted by Sun Thomas Sep 15, 2011

To fix this you need to override the OnPageIndexChanging event, something like this: protected override void OnPageIndexChanging(GridViewPageEventArgs e) { this.PageIndex = e.NewPageIndex; this.DataBind(); } This is on the DataPagerGridView class.

Posted by Ignacio Soto Mar 15, 2011

i have try to make same web control for me but i can not add IPageableItemContainer in my .cs file please give me reply

thanks

Posted by pinkesh patel Sep 23, 2010

Excelent article. Congratulations and thanx

Posted by Jaime Avila Jul 28, 2010

tt

Posted by ravi garg Mar 09, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor