Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Adjust Page Number when Page Length Changes (See related posts)

In a web page you are display a certain page worth of results. If you allow the user to change the page length, you'll need to find the new page number that contains the first item on the current page. This is VB.Net code.

intOldPageLength = GetPageLengthValue()

' if there is a new page length value
If intOldPageLength <> m_intPageLength Then

   ' adjust the page number to compensate
   intItemCount = ((m_intPage - 1) * intOldPageLength) + 1
   m_intPage = intItemCount \ m_intPageLength
   If intItemCount - (m_intPage * m_intPageLength) > 0 Then
      m_intPage = m_intPage + 1
   End If

End If


The index of the first item on the current page happens to be a count of the items up to that point. So assume we are paging that amount and use the last page as our new current page. We apply standard paging logic of items integer divided by page length to determine the number of pages and add one if there is a remainder.

There are a couple formulas at work here worth noting.
Index of Last Item on page:
page number * page length
Index of First Item on page:
(index of last item of previous page) + 1
((page number - 1) * page length) + 1
Page Count based on page length:
number of items [integer divide] page length
if there is a remainder to the above then add 1

You need to create an account or log in to post comments to this site.


Click here to browse all 4852 code snippets

Related Posts