[thelist] ASP.NET: How to do paging? (long version)

Casey aspnet at thecrookstons.com
Mon Dec 5 16:17:13 CST 2005


Here goes the long version of my post.  I'm doing a site which pulls some
products from a database and displays them.  Simple enough.

The catch is that we need to only show 15 products at a time from a
collection of ~ 45 to ~150 depending on category.  Then at the bottom of the
page we give them some standard paging links:  << Back 1 | 2 | 3 Forward >>.
Everything is working fine but one little thing: I need to bold the page
is actually being displayed.  That should be simple enough too, but
about the way I have structured my code is making it hard.

There is a test site at:  This is still a
work in progress and I know there are aspects of it that need work - I am
aware of all of them.  The issue I need help on is the one described in this

Here's the basic layout of the entire page:

Sub Page_Load(sender As object, e As EventArgs)
    if not ispostback then
        BuildImages() ' build a DataTable and cache it
    end if
End Sub

Function BuildImages() As DataView
    loop through a DataRecord, adding each item to a DataTable
    cache the dataTable
    define ViewState("totalPages") as total products/15 rounded up.
    return DataView
end Sub

Sub NewOnPage(sender as Object, e as CommandEventArgs)
    Dim onPage as integer = e.CommandArgument
    ViewState("onPage") = onPage
End Sub

Sub FilterAndBind()
    load the cached DataTable into a dataView
    filter the dataView based ViewState("onPage")
    bind dataView to a dataList for display
End Sub

Sub BuildPagingLinks(totalPages)
    create a placeHolder
    create a linkButton
    add text to link button.  ex: "<< Back"
     lbtnLinkButton.CommandArgument = onPage-1
     AddHandler lbtnLinkButton.Command, AddressOf Me.NewOnPage
    ' used Google to figure out how to do the two lines above
    add linkButton to placeHolder
End Sub

    <dataList> ' displays products
    <placeholder> ' displays paging links

In English, here's the logic:  The first time the page is loaded we build a
DataTable (which by def. is held in memory) and then we cache it.  Then, we
go to the sub wich places the dataTable into a DataView and sorts the View
based on what page we are on.  On the intial lode, it's page 1.  Then, from
within the page load function, we call the sub which builds the paging
links.  Each link in the paging link is set to send a value to the sub
NewOnPage.  Now that all of this is done, we load the page.

Now, if a user clicks on any link inside the paging links, a value is sent
to NewOnPage.  The value is either OnPage-1, onPage+1, or a raw number such
as 5.  NewOnPage does nothing but set the value of ViewState("onPage") to a
new number.

THE PROBLEM is that BuildPagingLinks, which is called from the Page Load
sub, always gets executed BEFORE NewOnPage when a user clicks on a paging
link.  The result is that the sub BuildPagingLinks always thinks we are one
page behind where we actually are.  Does that make sense?

BuildPagingLinks is executed BEFORE ViewState("onPage") is refreshed.
NewOnPage is executed which refreshes ViewState("onPage").

I want to bold the page we are on while building the paging links from
within  BuildPagingLinks, it always shows the page we were just on, and not
the page we ARE on.

Possibile solutions:

1) Do not use LinkButtons inside the paging.  Just use regular links, do a
page re-load instead and pass the values in a query string.   But, that
defeats the purpose of using a DataTable which is kept in memory.  I don't
WANT to reload the page, I just want to submit a form and do a postback.
That's why I asked the first question about these being cached in the
ViewState or Session. (asp.net: Easy DataTable Question)

2) Do not execute BuildPagingLinks from with Page Load.  Do it:
        a) from within FilterAndBind.  This seemed the most obvious choice,
and I tried it first.  An odd thing happens.  The first time the page is
loaded it works  perfectly, but after clicking on a linkButton from inside
the paging links, the sub NewOnPage (to which the paging values are being
sent) never even fires.  The  result, of course, is that FilterAndBind and
BuildPagingLinks don't fire either.  I have NO idea why, but it lead to my
third post....

        b) just call BuildPagingLinks at the very end of the <script
runat=server> after everything else was done.  But, I cou'd not figure out
how to do this, either from the books or from Google.  Thus my second post.
(asp:net Call a sub from within <script>)

3) Don't use linkButtons, but still force a post back instead of page
re-load.  Thus my third post.  (asp.net: A better way to post back from

And there you have it.  I am sure that my overall logic of how to build the
page is slightly off, and I am open to suggestions.  If you are still
reading, and if you can help, I sure would be in your debt.

Thank you,


More information about the thelist mailing list