Pagination function for search page returning too many hits to display on one page.

H

Henrik

Hello all.

I'm after some sort of pagination function.
One that takes some in data that I provide, and returns an array of
controls, pointing to page 2, page 3 etc.
Trying to mimic the way that Google handles the pagination when a lot of
pages are returned.
That is:

If you are at page 1 of 100 possible pages, you want to see the following:
1 2 3 4 5 6 7 8 9 10 >>

If you are at page 2 of 100 possible pages, you want to see the following:
<< 1 2 3 4 5 6 7 8 9 10 >>

If you are at page 22 of 100 possible pages, you want to see the following:
<< 18 19 20 21 22 23 24 25 26 >>

If you are at page 100 of 100 possible pages, you want to see the following:
<< 93 94 95 96 97 98 99 100

If you are at page 1 of 7 possible pages, you want to see the following
(don't display the pages that don't exist, ie 8, 9 and 10):
1 2 3 4 5 6 7 >>

If you are at page 2 of 7 possible pages, you want to see the following
(don't display the pages that don't exist, ie 8, 9 and 10):
<< 1 2 3 4 5 6 7 >>

If you are at page 7 of 7 possible pages, you want to see the following
(don't display the pages that don't exist, ie 8, 9 and 10):
<< 1 2 3 4 5 6 7

I'm after a function that looks like this:

public System.Web.UI.Control[] GetPageLinks(int TotalDisplayItems, int
CurrentPage, int DisplayItemsPerPage, int MaxNumberofLinks)
{
....
}

int TotalDisplayItems // Total number of items to display over the pages
int DisplayItemsPerPage // Number of items to show per page (for instance
10, 15, 25 eller 50)
int CurrentPage // The page currently displayed

int MaxNumberofLinks // Max number of links to get back from function

With that I can then loop the Control Array, and perfom the code that adds
the links at the bottom of the page, like this:

TableCell oCell = new TableCell();
foreach (System.Web.UI.Control oControl in oControls)
{
oCell.Controls.Add(oControl);
oCell.Controls.Add(new System.Web.UI.LiteralControl(nbsp;nbsp;));
}

Is this something someone has done previously and is willing to share?

I know that the data I have is sufficient for the function,
but when I'm trying to write the function I end up getting dizzy :)
trying to catch all the possible combinations.
I gave up after 9 separate if clauses, with a switch clause inside each and
everyone.

// Henrik
 
P

Phillip Ian

The good news is, I just wrote something quite similar a couple of
weeks ago...

The bad news is, I'm using VB, not C#.

In the hopes that VB is as simple as everyone says and you can extract
the methodology out of it, here's what I used:

Private Sub BuildNavBar(ByVal ctl As ControlCollection)
ctl.Clear()

' The lower bound
Dim lower As Integer = (Floor((pageno - 1) / 10) * 10) + 1
If lower < 1 Then lower = 1

' The upper bound
Dim upper As Integer = lower + 9
If upper > cat.PageCount Then upper = cat.PageCount

' Draw the nav bar
Dim l As HyperLink
If lower > 1 Then
' Add the ... in front
l = New HyperLink
l.Text = "..."
l.NavigateUrl = "browse.aspx?cat=" & cat.ID & "&pageno=" &
Max(pageno - 10, 1)
l.CssClass = "ProdPager"
ctl.Add(l)
ctl.Add(New LiteralControl("&nbsp;&nbsp;"))
End If
For i As Integer = lower To upper
If i = pageno Then
' The current page
ctl.Add(New LiteralControl(pageno))
If i <> cat.PageCount Then ctl.Add(New
LiteralControl("&nbsp;&nbsp;"))
Else
l = New HyperLink
l.Text = i
l.NavigateUrl = "browse.aspx?cat=" & cat.ID & "&pageno=" & i
l.CssClass = "ProdPager"
ctl.Add(l)
If i <> cat.PageCount Then ctl.Add(New
LiteralControl("&nbsp;&nbsp;"))
End If
Next
If upper < cat.PageCount Then
' Add the ... in back
l = New HyperLink
l.Text = "..."
l.NavigateUrl = "browse.aspx?cat=" & cat.ID & "&pageno=" &
Min(pageno + 10, cat.PageCount)
l.CssClass = "ProdPager"
ctl.Add(l)
End If
End Sub

Note that I use a page count, not an item count, and I've hard-coded
the # of links to show, but these should be easily changed to suit your
application.

Hope it helps,
-Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top