Solved

create a paging function

Posted on 2010-09-16
7
232 Views
Last Modified: 2012-05-10
Im using ajax to take a result of html code to a webpage. Let say items in a eshop.

Code behind the webservice take the requested data and putting it into a html content. The ws return a plain html string that formats the page.

I want to create some sort of pager of this, that get a specifik amount of the items, but need little help with the idea of it.

Thanks in advanced,
0
Comment
Question by:dingir
  • 2
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 33698898
If you are using LINQ or can use it, it's very easy:

Just pass the parameters pageIndex and pageSize along to the codebehind from there you can do:


pageResultSet = yourCollection.Take(pageSize).Skip(pageIndex*pageSize);

Open in new window

0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 33698909
Not difficult, just define the number of items per page and add a parameter to the function that returns the items. That new parameter represents the page number. If you have the whole items collection stored in session, in example, then apply the parameter to retrieve just the selected items based on this formula:

first element to retrieve = (page number - 1) * items per page
last element to retrieve = (first element + items per page) - 1

Let's see with a sample:
Imagine that you have loaded Session("Items") with a System.Collections.Generic.List(Of MyItem) objects. It doesn't matter the number of items stored. So, the first time that you show your products to the user you'll show the first page, obviously. So

Session("CurrentPage") = 1

And don't forget:
Session("ItemsPerPage") = 5 'For example

In your function that returns the items to the page (well, really you don't return the items, but compose a HTML string, but it doesn't matter) you need a parameter:

Public Function GetItemsHTMLString(ByVal pageNumber As Integer) As String
    Dim firstItem As Integer = (pageNumber - 1) * DirectCast(Session("ItemsPerPage"), Integer)
    Dim lastItem As Integer = (firstItem + DirectCast(Session("ItemsPerPage"), Integer)) - 1
    If lastItem > (DirectCast(Session("Items"), List(Of MyItem)).Count - 1) Then
        lastItem = (DirectCast(Session("Items"), List(Of MyItem)).Count - 1)
    End If
    For k As Integer = firstItem To LastItem
        Dim theItem As MyItem = DirectCast(Session("Items"), List(Of MyItem))(k)
        'Now use MyItem to compose the HTML string
    Next

End Function

Well, I think that you can make a global idea with this little help.

Hope that helps.
0
 
LVL 1

Author Comment

by:dingir
ID: 33698929
Thank's

This is very easy if I store some sort of "whole collection" somewhere. Though, I can't see that as a nice way to develope.

Skip.. is that exactly the same as take the resultset (as tolist) but stripping of the first xx items? if that's the case, I can do the pager with

getList().Skip(10).Take(10)

to recieve the 10-19 items of the total collection? I just need to send the skip/take parameters through the webservice-method..

?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 33698959
Storing a whole collection when paging is a common practice. Note that you don't have to store all your items properties (overloading your memory) in the collection, but for example only their unique indexes.

The reason for this is that your real back-end collection might change at any time (commonly). Imagine a list of products. Your user wants to watch (paged) the list of products, so you get a snapshot of your current list and do the pagination as described in my previous post. In this way, storing a whole collection in memory, you can do the pagination knowing certainly that the product list that you offer to the client will not change during all the pagination process.

If you don't have a whole collection in memory, what happens if the admin adds a new product while the client is watching the list? For example, I'm watching a list of 50 products in 5 pages of 10 items and suddenly I find that there are 6 pages (the last one with just 1 item).

That's why when you're paging, is always a good idea to define your start-end limits ("I'll show this X articles in X pages of X items per page"). If the client makes another query later, then you'll have the time to update your items list, if it proceeds.
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33699018
dingir your assumptions on skip and take are correct :). They do exactly that.
0
 
LVL 1

Author Comment

by:dingir
ID: 33699108
Roland, your absolutely right.

Though, in that described case, I would gladly take the new items in the resultset, which also make the queries more stable in case of deleted items.

I'm really not sure when i would prefer the use of "cached indexes" other then in thinking of security. I.e. if have to make sure that actions can't be done outside the fetched items. You know, to avoid i.e. querystring-digging (think: if item=40 works, i would probably try item=41, and it works also). I may think wrong here..

Existenz,
thank's than this is the absolutely most preferred way for me! Thank's.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now