ASP.NET - DataPager and PageSize. - Asked By Ryan Gomez on 13-Aug-09 08:16 AM

Hi everyone,

I'm using ListView and DataPager to list down 10 records per page. I'm using the numeric style to page my records as shown below:

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>


However, in some cases the records returned are less than the PageSize. The paging interface appears neverthless (in my case at the bottom) and the number 1 can be seen at the bottom of the page. Is there anyway of "turning on" DataPager only when the records are more than the PageSize without working on the code-behind (i.e. only calling DataPager when records are more than PageSize)? Any nudges in the right direction would be very welcome. Cheers

Ryan

You need to override the default behaviour using the "TemplatePagerField"

[)ia6l0 iii replied to Ryan Gomez on 13-Aug-09 01:53 PM
With the help of this, you can manipulate the paging interface and set appropriate actions in the listview and the datapager.

A sample field collection would look like,

<asp:TemplatePagerField>
<PagerTemplate>
<b>Page <asp:Label runat="server" ID="CurrentPageLabel" Text="<%# Container.TotalRowCount>0 ?
(Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
of
<asp:Label runat="server" ID="TotalPagesLabel" Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
(<asp:Label runat="server" ID="TotalItemsLabel" Text="<%# Container.TotalRowCount%>" />
records)
</PagerTemplate>
</asp:TemplatePagerField>

To set the properties from code-behind, you would need to have the following values:
a) DataPager control's Pagesize
b) The current page index

and then depending on your requirement, you would do something like below in the prerender event.
int PageSize = dataPagerCtrl.PageSize;
dataPagerCtrl.SetPageProperties(CurrentPage * PageSize, PageSize, true);


Jules replied to Ryan Gomez on 16-Jun-11 10:55 AM
Excellent Example. Thank You