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

Discussion in 'ASP .Net' started by Henrik, Jun 2, 2005.

  1. Henrik

    Henrik Guest

    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
     
    Henrik, Jun 2, 2005
    #1
    1. Advertising

  2. Henrik

    Phillip Ian Guest

    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
     
    Phillip Ian, Jun 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dos-Man
    Replies:
    5
    Views:
    1,274
    Augustus
    Mar 4, 2004
  2. Adam Balgach
    Replies:
    2
    Views:
    577
    news-east
    Nov 28, 2004
  3. John Gordon
    Replies:
    5
    Views:
    1,556
    John Gordon
    Dec 8, 2006
  4. Garyrek

    how many page hits specified date range

    Garyrek, Apr 1, 2004, in forum: ASP General
    Replies:
    10
    Views:
    266
    Aaron Bertrand [MVP]
    Apr 9, 2004
  5. Evolution
    Replies:
    1
    Views:
    115
    Evolution
    Apr 15, 2011
Loading...

Share This Page