How to find current, next, previous item on a list of objects using data item index

I need to add buttons for previous and next on a page

The buttons need to move back and forth on a list of objects (orders)

The list of orders is stored in a session variable

orderList.GetOrdersByDate(CDate(Me.startDate.Text), CDate(Me.endDate.Text).AddDays(1), strEnv)
Session("orderList") = orderList.Items

Open in new window


The name of the field to be used as the navigation parameter is OrderId

How can this be done?
the_lone_noteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Assuming that the session list is a representative live of the items.  You could probably use something like (Note this is untested):
' Assuming that you retrieved an order id and have stored it as currentOrderID
Dim currentOrder = (From order in orderList.Items Where order.OrderID.Equals(currentOrderID) Select order).SingleOrDefault()
' I dont know what your Order object is so I just used As Order
Dim previousOrder As Order = Nothing
' I dont know what your Order object is so I just used As Order
Dim nextOrder As Order = Nothing

If currentOrder IsNot Nothing Then
	' I don't know if the object representing your orders support the Linq LAST method, if it does then you do not 
	' have to use Order By order.OrderID Descending, you can just call LastOrDefault().
	previousOrder = (From order in orderList.Items Where order.OrderID < currentOrderID Order By order.OrderID Descending Select order).FirstOrDefault()
	nextOrder = (From order in orderList.Items Where order.OrderID > currentOrderID Select order).FirstOrDefault()

	' Perform any other logic here needed to fill in fields and such for the current order display.
End If

If previousOrder IsNot Nothing Then
	' If previousOrder is not null then you would show your button and set up it's link here
End If

If nextOrder IsNot Nothing Then
	' If nextOrder is not null then you would show your button and set up it's link here
End If

Open in new window

-saige-
0
CodeCruiserCommented:
Another option is to get all orderids into an array and then increment/decrement array index in next/previous buttons.
0
the_lone_noteAuthor Commented:
To CodeCruiser,

How would the incrementation occur in the oders list/ array by using orderid? Will this be a for loop?

Would you be able to show a small code example?

Many thanks.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

the_lone_noteAuthor Commented:
To it_saige,

Many thanks for your explanation, I have tried it, and works ok, but the sequence up/ down, is not following the sequence in the items list.

How can this be corrected?

This is how I have implemented your recommendation this far (previous is down, next is up):

    Protected Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click
        Dim currentOrderID As String
        Dim nextOrder As Order = Nothing
        spiderSiteKey = Request.QueryString("siteKey")
        strEnv = Request.QueryString("strEnv")
        orderList = New Orders(spiderSiteKey)
        orderList.GetOrdersByDate(CDate(Request.QueryString("Start")), CDate(Request.QueryString("End")).AddDays(1), strEnv)
        currentOrderID = Request.QueryString("order_id")
        Dim currentOrder = (From order In orderList.Items Where order.OrderID.Equals(currentOrderID) Select order).FirstOrDefault()
        If currentOrder IsNot Nothing Then
            nextOrder = (From order In orderList.Items Where order.OrderID < currentOrderID Order By order.OrderID Descending Select order).FirstOrDefault()
            If nextOrder IsNot Nothing Then
                btnUp.Visible = True
                Response.Redirect("~/order-review.aspx?order_id=" & nextOrder.OrderID _
                    & "&siteKey=" & spiderSiteKey _
                    & "&strEnv=" & strEnv _
                    & "&Start=" & CDate(Request.QueryString("Start")) _
                    & "&End=" & CDate(Request.QueryString("End")).AddDays(1))
            Else
                btnUp.Visible = False
            End If
        End If
    End Sub

    Protected Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        Dim currentOrderID As String
        Dim previousOrder As Order = Nothing
        spiderSiteKey = Request.QueryString("siteKey")
        strEnv = Request.QueryString("strEnv")
        orderList = New Orders(spiderSiteKey)
        orderList.GetOrdersByDate(CDate(Request.QueryString("Start")), CDate(Request.QueryString("End")).AddDays(1), strEnv)
        currentOrderID = Request.QueryString("order_id")
        Dim currentOrder = (From order In orderList.Items Where order.OrderID.Equals(currentOrderID) Select order).FirstOrDefault()
        If currentOrder IsNot Nothing Then
            previousOrder = (From order In orderList.Items Where order.OrderID < currentOrderID Order By order.OrderID Descending Select order).FirstOrDefault()
            If previousOrder IsNot Nothing Then
                btnDown.Visible = True
                Response.Redirect("~/order-review.aspx?order_id=" & previousOrder.OrderID _
                                    & "&siteKey=" & spiderSiteKey _
                                    & "&strEnv=" & strEnv _
                                    & "&Start=" & CDate(Request.QueryString("Start")) _
                                    & "&End=" & CDate(Request.QueryString("End")).AddDays(1))
            Else
                btnDown.Visible = False
            End If
        End If
    End Sub

Open in new window

0
it_saigeDeveloperCommented:
How is the sequence determined in the items list?  I ordered by OrderID because I assumed that your list was ordered by id.  If there is some other determining factor then ordering by that property should resolve the issue.

If it's not ordered by anything, then you may need to use Index based resolution.

-saige-
0
it_saigeDeveloperCommented:
Index based would be something like:
Dim currentOrderID As String
Dim previousOrder As Order = Nothing
Dim nextOrder As Order = Nothing
spiderSiteKey = Request.QueryString("siteKey")
strEnv = Request.QueryString("strEnv")
orderList = New Orders(spiderSiteKey)
orderList.GetOrdersByDate(CDate(Request.QueryString("Start")), CDate(Request.QueryString("End")).AddDays(1), strEnv)
currentOrderID = Request.QueryString("order_id")
Dim currentOrder = (From order In orderList.Items Where order.OrderID.Equals(currentOrderID) Select order).FirstOrDefault()
If currentOrder IsNot Nothing Then
	Dim index = orderList.Items.IndexOf(currentOrder)
	If Not index <= 0 Then previousOrder = DirectCast(orderList.Items(index - 1), Order)
	If Not index >= orderList.Count - 1 Then nextOrder = DirectCast(orderList.Items(index + 1), Order)

	If previousOrder IsNot Nothing Then
		btnDown.Visible = True
		Response.Redirect("~/order-review.aspx?order_id=" & previousOrder.OrderID _
		    & "&siteKey=" & spiderSiteKey _
		    & "&strEnv=" & strEnv _
		    & "&Start=" & CDate(Request.QueryString("Start")) _
		    & "&End=" & CDate(Request.QueryString("End")).AddDays(1))
	Else
		btnDown.Visible = False
	End If

	If nextOrder IsNot Nothing Then
		btnUp.Visible = True
		Response.Redirect("~/order-review.aspx?order_id=" & nextOrder.OrderID _
		    & "&siteKey=" & spiderSiteKey _
		    & "&strEnv=" & strEnv _
		    & "&Start=" & CDate(Request.QueryString("Start")) _
		    & "&End=" & CDate(Request.QueryString("End")).AddDays(1))
	Else
		btnUp.Visible = False
	End If
End If

Open in new window

Personally though, I would do this in the Page_Load event.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
You can export the list to an array and then use array index (0 to length - 1) instead of OrderID for navigation.
0
the_lone_noteAuthor Commented:
Thank you much for your help.

I ended up using your index based solution and it works nicely.

Now I have another request, the first page will pass a sort by parameter via query string to the second page (the one you helped me with), and the pagination must follow the sort by parameter order.

This is a topic for a different question, I will be posting shortly. Hopefully I will be able to get some more help.

Many thanks to both experts.
0
it_saigeDeveloperCommented:
Did you get your other question solved?  I was working on a method and the question was deleted.

-saige-
0
the_lone_noteAuthor Commented:
sorry it_sage,

I was updating the code because I was working on it, the results are still a bit messy, not just for taking the sort parameters (attribute and direction) but for the regular paging as well. I think the problem resides in how the current records gets set. But that is just a guess of my part.

This is the updated new question: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28675957.html

Many thanks for your help..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.