Ryan McDonnell - Pursuing Web Application Zen

Always show the PagerTemplate in GridView control

Wednesday, August 1st, 2007 at 6:00 am

The PagerTemplate can be a handy place to put some frequently used controls or data in your GridView. I like to use the area to include the number and range of records displayed, some paging details and a DropDownList to let the user view more records at a time.

Sample PagerTemplate output

But what happens when the number of records doesn’t exceed the PageSize? The PagerTemplate isn’t displayed.

To get around that, extend the GridView in a custom server control and override the PageCount property to force the PagerTemplate to always be displayed. As long as the PageCount property is greater than one, the paging controls are rendered.

public override int PageCount {
 
get {
    // Override the PageCount if only one page exists to
   
// force the PagerTemplate to always be displayed
    int pageCount = base.PageCount;
    if (pageCount == 1) {
      // Only override the PageCount if the GridView.CreateChildControls is calling
     
System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame(1);
      if (sf.GetMethod().Name == "CreateChildControls" && sf.GetMethod().ReflectedType == typeof(GridView))
      {
        pageCount++;
      }
    }
    return pageCount;
  }
}

The key part here is the StackFrame is looking up the stack and only returning an inflated PageCount if the calling method is the CreateChildControls method from the GridView. This keeps our PageCount number valid for display purposes and only tricks the GridView into thinking there is more than one page for purposes of forcing the paging controls to always be displayed.

9 Comments

  1. Polisetty

    You could also say in the databound to keep bottompagerrow.visible = true.

  2. Ryan McDonnell

    I could have sworn that I tried that but it didn’t work. But I just tested it again and it does work.

    Kudos.

  3. andrew

    Look, all that nonsense and reflection isn’t required. A great attempt but not a very elegant solution. vb.net code =) CHEERS!

    Protected Overrides Function CreateChildControls(ByVal dataSource As Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
    Dim result As Int32 = MyBase.CreateChildControls(dataSource, dataBinding)
    If Me.TopPagerRow IsNot Nothing AndAlso Me.AlwaysShowPager Then
    Me.TopPagerRow.Visible = True
    End If
    Return result
    End Function

  4. andrew

    by the way, AlwaysShowPager is a boolean i added, but it isn’t really required it just allows you to turn always show on and off. Which is of course required for some re-use cases…

  5. Ryan McDonnell

    Amazing how we can overlook the simplest of solutions at times. :-)

    Thanks for the VB code, Andrew.

  6. andrew

    No problem, just so you know, i was looking around for a while trying to figure this out but after i saw your solution it tracked me down to the exact point in the reflected gridview code that i needed to understand. I just twisted your solution slightly. Thanks for doing the leg work!!!

  7. Umair Hafeez

    I have done a little thing.

    After calling the GridView.DataBind()

    just write the following line.

    GridView.TopPagerRow.Visible = True
    OR
    GridView.BottomPagerRow.Visible = True

    This does the trick for me.

  8. Peter Duerden

    Just what I needed but required in C#. I have therefore made te following conversion and also included a check for the bottom pager row:

    protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
    {
    int returnValue = base.CreateChildControls(dataSource, dataBinding);

    if (this.TopPagerRow != null && this.AlwaysShowPager)
    {
    this.TopPagerRow.Visible = true;
    }

    if (this.BottomPagerRow != null && this.AlwaysShowPager)
    {
    this.BottomPagerRow.Visible = true;
    }

    return returnValue;
    }

    Thanks.

  9. Shiji Joseph

    I tried what Umair Hafeez mentioned, and its working perfectly. I believe that is the best way to go about… less code less confusion and easier to understand.

    Cheers!

Leave a Comment

The following tags are allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Ryan McDonnell

I am a web application developer living in southern California. Commonly called a “jack of all trades” by collegues, I constantly strive to broaden my knowledge into other fields.
More about Ryan McDonnell »


 Subscribe in a reader

Quick Links & Notes

© 2004-2008 Ryan McDonnell. View my profile on LinkedIn
Some rights reserved under the Creative Commons Attribution-Share Alike 3.0 United States License.