Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide DataPager if Pages = 1

How do I hide the DataPager if there is only one page of data?

In the DataPager events I have a asp:Button when clicked it gets records.

Sometimes there will be only one record and I need to hide the pager if there is one record.

It could be done on postback but I don't know what property is for the page count.

like image 666
ONYX Avatar asked Feb 17 '12 01:02

ONYX


1 Answers

There is a blog article on MSDN that covers this topic:

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

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);
}
like image 191
dee-see Avatar answered Oct 06 '22 00:10

dee-see