Always show the PagerTemplate in GridView control
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.
![]()
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.
You could also say in the databound to keep bottompagerrow.visible = true.
I could have sworn that I tried that but it didn’t work. But I just tested it again and it does work.
Kudos.
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
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…
Amazing how we can overlook the simplest of solutions at times.
Thanks for the VB code, 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!!!
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.
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.
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!