Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create a paging function

Posted on 2010-09-16
7
Medium Priority
?
241 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
Independent Software Vendors: 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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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