Solved

create a paging function

Posted on 2010-09-16
7
236 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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