MaintainScrollPosition when returning to a page


Is there a way to maintain scroll position like MaintainScrollPositionOnPostBack(), but for when you hit the back button?  So say you were looking at a datalist, clicked on an item to go to the details page and then hit the back button.  Right now it goes back to the top of the list.  Does anyone know how to get it to return to the exact position on the page that you were at before?  Thanks!
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.

You will need to do some client side scripting with javascript and put those values in the session variable. That should fix your problem.

Some JScript examples:
Look here:

or here:

download the code...compile it and add to your Toolbox. Then just drag and drop it on to your page and that's it.

To download the code go to:

To read the article go to:
brianpsmith81Author Commented:
aki4u, that looks like a valid solution, but a couple concerns before I try implementing....I am running 2.0 and using vb, not c.  Will it still work?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Let's clear the situation, this is your situation right?:
- (A) Main Page with datalist
- (B) Details Page with detail information from clicked item in a datalist

And when doing A -> B -> A you have the problem that your scrollbar is position back on top and not on the datalist?

If that's the case:
You are not doing a postback, so the solution aki4u gave won't work. You will need to read the current scrollbar position on the item click in the datalist. Put those values inside a hidden textbox with javascript and then inside a Session object in the click event. Then you get redirected to the details page. You click on back and the Main page will read the session object and set the scrollbar back on the correct position.

First, let me ask you if you have in your page_load event: If Page.IsPostBack Then Exit Sub

Like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then Exit Sub

        'your code here
    End Sub
brianpsmith81Author Commented:
existenz2, yes that is the case, so that looks like maybe what I'll have to do, thanks for clarifying that.

aki4u, not exactly, no...I have the maintain scroll back before the other code in the Page_Load() event.

 Page.MaintainScrollPositionOnPostBack() = True
        If Not IsPostBack Then
         .....other code
        End If

The reason I asked that is because I wrote a sample and run it without any problems. I used same scenario as you have. Now, if you are binding data when you click browser back button, then it's not going to work. I will check the scenario with control that I recommended and I will let you know.

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
brianpsmith81Author Commented:
I am binding data if it isn't a postback, it first checks the user login information and then it calls a LoadOrders subroutine which loads the datalist.  So I'm guessing that existenz2 is right, I'll have to save the scrollbar position to a session variable.  If I don't have to though, that would be preferred.
brianpsmith81Author Commented:
BTW, I have filters and paging set up for the datagrid, and it remembers that (any search filters, what 'page' the datalist is on, etc.) when I click the back button, but not the scrollbar location.  However, what's weird is if I have changed the paging (the paging links are at the bottom) it will come back with the scroll bottom at the bottom of the page, if not, it will come back at the top.  So it seems to remember the scrollbar position of the last subroutine action taken, but not of a plain link (<a href...>).  Will it maybe work if I change the link to an <asp:Hyperlink...> tag?
I would give that a try brian.. Nothing to loose right? But your findings do support that idea.
brianpsmith81Author Commented:
Yeah, that seemed to work.  Well, it looks like I answered my own question...too bad I can't give myself the points, haha.  I'll split them up between you guys though, I appreciate the help.  Thanks!

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

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.