How to hide a DataPager control when there is only one page of data

How to hide a DataPager control when there is only one page of data

  • Comments 6

When you are paging data in a ListView control using the DataPager, by default, the DataPager will be shown even if there is only one page of data. So, if you're using a NumericPagerField for example, you're going to end up with a text showing 1 in your page. So, in this cases, you might want to hide the DataPager control.
One way of achieving this is to change the visibility of the control on the DataBound event of the ListView control. For example:

protected void ListView1_DataBound(object sender, EventArgs e)
{
  DataPager1.Visible = (DataPager1.PageSize < DataPager1.TotalRowCount);
}

In the example above, the DataPager is not inside the ListView control. If you place the DataPager inside the LayoutTemplate, then you have to tweak the code a little bit to find the control inside ListView. For example:

protected void ListView1_DataBound(object sender, EventArgs e)
{
  DataPager pager = (DataPager) ListView1.FindControl("DataPager1");
  pager.Visible = (pager.PageSize < pager.TotalRowCount);
}

This way you can control whether you want the pager displayed or not.

Leave a Comment
  • Please add 5 and 7 and type the answer here:
  • Post
  • Nice!

    a simple yet elegant solution!

  • Great Example.

    I like your short and straightforward writing style.

    It worked for me - Thanks!

  • I was looking for a good code for a while.

    This looks great.

    Thanx.

  • Nice and clean !

    I was wondering if there's a way to do it using styles rather than c# code.

    Thanks.

    Sanaa

  • Thanks, but this fails if ListView is empty. You should surround it with if (ListView1.Items.Count > 0).

  • Any idea for an MVVM Version?

Page 1 of 1 (6 items)