Results 1 to 9 of 9

Thread: Navigating records next/previous

  1. #1

    Thread Starter
    Registered User
    Join Date
    May 2013
    Posts
    3

    Navigating records next/previous

    Hi,

    I have written a .net web application quite some time ago and now required some enhancement... but I'm now lost on how to do this So appreciate any insights from all of you here.

    I have a page (page A) displaying a GridView of records from a SQL table. Upon clicking on a row, user will be lead to another page (page B) displaying the details of the selected record. From page A, I passed the value of a generated ID through querystring to page B, and based on this ID, the application will query the database to fetch all the values and display accordingly in a FormView. Page B will also allow user to edit and update the record.

    How is it possible if I want to have the ability to navigate to next/previous record from Page B without having to go back to Page A again?

    Appreciate any help! Thanks in advance!!

    Cheers!

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Navigating records next/previous

    Hey,

    Is it possible that you can show the URL that you pass into the details page?

    It might be as simple as looking at the id parameter that you pass in, and then constructing a new URL, in the form of a hyperlink, with the same URL, with the Id decremented and incremented by 1.

    Gary

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Navigating records next/previous

    Assuming that you have increasing numeric IDs, you can find the next record like this:
    sql Code:
    1. SELECT TOP 1 *
    2. FROM MyTable
    3. WHERE ID > @ID
    and you pass the ID of the current record to the @ID parameter. The previous record is similar:
    sql Code:
    1. SELECT TOP 1 *
    2. FROM MyTable
    3. WHERE ID < @ID
    4. ORDER BY ID DESC
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Navigating records next/previous

    Quote Originally Posted by gep13 View Post
    Hey,

    Is it possible that you can show the URL that you pass into the details page?

    It might be as simple as looking at the id parameter that you pass in, and then constructing a new URL, in the form of a hyperlink, with the same URL, with the Id decremented and incremented by 1.

    Gary
    I don't think that that would be safe because, while there generally shouldn't be any gaps in the ID sequence, there might be. Even if you never delete a record, an error during an insert will mean that the current ID will not end up being used.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Navigating records next/previous

    Quote Originally Posted by jmcilhinney View Post
    I don't think that that would be safe because, while there generally shouldn't be any gaps in the ID sequence, there might be. Even if you never delete a record, an error during an insert will mean that the current ID will not end up being used.
    Ah, you are of course correct!

    Gary

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Navigating records next/previous

    Quote Originally Posted by jmcilhinney View Post
    Assuming that you have increasing numeric IDs, you can find the next record like this:
    sql Code:
    1. SELECT TOP 1 *
    2. FROM MyTable
    3. WHERE ID > @ID
    and you pass the ID of the current record to the @ID parameter. The previous record is similar:
    sql Code:
    1. SELECT TOP 1 *
    2. FROM MyTable
    3. WHERE ID < @ID
    4. ORDER BY ID DESC
    This might work but it is not a common practice to fetch the next record one by one, usually you will have to use a pager or the paging capabilities of the formview. So you either either bring everything to the formview and select the current record according to the DataKeyNames id of formview(i only suggest that for small record sets and easy of use, lower of 100 records at least) of you must look for paging examples that will bring portions of data to your page.Select TOP 10 * or TOP 20 * etc might work somehow also if you modify JMC example(for the pager). Finally there is always the possibility of using a web service and bind your data with jquery but i don't think formview will work in that case.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7

    Thread Starter
    Registered User
    Join Date
    May 2013
    Posts
    3

    Re: Navigating records next/previous

    Dear all,

    Thanks for all the suggestions.

    @gep13, the URL that was passed into Page B looks like this...card.aspx?mode=view&rec_num=war-2012-000041 <-- first 3 letters of the rec_num is abbreviation of a location; middle 4 digits is the year the record was created; and last 6 digits is running number for the entire year for that particular location.

    My best guess will be to pass over the previous rec_num and next rec_num but it means that in the GridView, I will have to be able to retrieve the previous and next record and populate accordingly.

    Anyone have any more idea?

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Navigating records next/previous

    Hey,

    I am not sure that passing in additional information in the query string is the best approach.

    I would suggest that John's approach is probably the best, unless you are looking to restructure the page, as suggested by sapator.

    Based on the incoming URL, query your database for the next and previous article number, and construct URL's to place on the page.

    Gary

  9. #9

    Thread Starter
    Registered User
    Join Date
    May 2013
    Posts
    3

    Re: Navigating records next/previous

    Hi Gary,

    Understood... well let me think how it can be done... probably have to look into restructuring the page. >.<

    Thank you and thanks all!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width