Solved

create a paging function

Posted on 2010-09-16
7
231 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now