SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    paging creates extra page

    I am performing "semi"-custom paging for my dynamic LINQ query. However, if the number of results returned exactly fill the page, then I wind up with one extra page that's empty and I can't figure out why. i.e. if I have a page size of 15 and 14, 21, 101, etc. items then everything is fine. However, if I have 15, 30, 45, 60, etc. then I get one extra blank page and I've been unable to make it stop. Anyone have some ideas except to add a new record when I see we're sitting on a multiple of the page size?

    I write out the paging control like this and it loops through one time too many if the last page is full...

    protected void WritePagingControlHtml(int inputNumberOfPages, int inputCurrentPageNumber)
    {
    // testing.Text += "<br /> Starting WritePagingControlHtml(int inputNumberOfPages)";

    string outputLiteralPageControlHTML = "";
    int displayNumberOfPages = inputNumberOfPages + 1;
    int displayCurrentPageNumber = inputCurrentPageNumber + 1;
    // testing.Text += "<br /> displayNumberOfPages = " + displayNumberOfPages;
    // testing.Text += "<br /> displayCurrentPageNumber = " + displayCurrentPageNumber;
    for (int loopDisplayPageNumber = 1; loopDisplayPageNumber < displayNumberOfPages + 1; loopDisplayPageNumber++)
    {
    string outputPagingLinkClass = "";
    if (loopDisplayPageNumber == displayCurrentPageNumber)
    {
    outputPagingLinkClass = " class=\"currentPage\"";
    }

    // testing.Text += "<br /> Before GetAppLink call: classFilterQueryStringParameter = " + classFilterQueryStringParameter;

    outputLiteralPageControlHTML += " <a href=\"" + GetAppLink(classSortQueryStringParameter, classFilterQueryStringParameter, loopDisplayPageNumber) + "\"" + outputPagingLinkClass + ">" + loopDisplayPageNumber.ToString() + "</a>";
    }

    litPagingControl.Text = outputLiteralPageControlHTML;
    }


    And I calculate the number of pages as part of the query code...

    cmschangeTestDataContext db = new cmschangeTestDataContext();
    IQueryable<cmschangesTest> query = db.cmschangesTests;
    int newRowCount = 0;

    if (inputFilterValue == "")
    {
    query = db.cmschangesTests
    .OrderBy(inputOrderValue)
    .Skip(skip)
    .Take(take);

    newRowCount = db.cmschangesTests.Count();
    }
    else
    {
    query = db.cmschangesTests
    .Where(inputFilterValue, System.DateTime.Now)
    //.Where(a => a.dontpublishbeforedate < System.DateTime.Now) // using lambda expression
    .OrderBy(inputOrderValue)
    .Skip(skip)
    .Take(take);

    newRowCount = db.cmschangesTests.Where(inputFilterValue, System.DateTime.Now).Count();
    }

    testing.Text += " <br /> inputFilterValue = \"" + inputFilterValue + "\"";
    testing.Text += " <br /> newRowCount = " + newRowCount.ToString();

    int inputNumberOfPages = newRowCount / numberOfContactsPerPage;

    WritePagingControlHtml(inputNumberOfPages, currentPageNumber);
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code Csharp:
    for (int loopDisplayPageNumber = 0; loopDisplayPageNumber < displayNumberOfPages + 1; loopDisplayPageNumber++)

    Start with "0"?

    Just a guess.

    EDIT: Might want to take the "+1" out of the "displayNumberOfPages" also.

  3. #3
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this

    if(newRowCount % numberOfContactsPerPage == 0)
    inputNumberofPages = newRowCount / numberOfContactsPerPage;
    Else
    inputNumberofPages = newRowCount / numberOfContactsPerPage + 1;

  4. #4
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chieftain View Post
    Try this

    if(newRowCount % numberOfContactsPerPage == 0)
    inputNumberofPages = newRowCount / numberOfContactsPerPage;
    Else
    inputNumberofPages = newRowCount / numberOfContactsPerPage + 1;
    This was basically it. I needed...

    if(newRowCount % numberOfContactsPerPage == 0)
    inputNumberofPages = newRowCount / numberOfContactsPerPage -1;
    Else
    inputNumberofPages = newRowCount / numberOfContactsPerPage;

    The integer division rounds at the wrong mark for my code, and I needed to manually adjust it.

    Thanks for the help.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  5. #5
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •