Solved

create a paging function

Posted on 2010-09-16
7
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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