• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

MaintainScrollPosition when returning to a page

Experts,

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!
0
brianpsmith81
Asked:
brianpsmith81
  • 5
  • 3
  • 3
2 Solutions
 
existenz2Commented:
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:
http://codepunk.hardwar.org.uk/ajs02.htm
http://lists.evolt.org/archive/Week-of-Mon-20031117/151687.html
http://www.codelifter.com/main/tips/tip_019.shtml
0
 
aki4uCommented:
Look here:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21768630.html

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:
http://aspnet.4guysfromrolla.com/code/sr.scroll.zip

To read the article go to:
http://aspnet.4guysfromrolla.com/articles/111704-1.aspx
0
 
brianpsmith81Author Commented:
aki4u, that looks like a valid solution, but a couple concerns before I try implementing....I am running asp.net 2.0 and using vb, not c.  Will it still work?
0
Technology Partners: 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!

 
existenz2Commented:
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.

0
 
aki4uCommented:
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
0
 
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

0
 
aki4uCommented:
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.
0
 
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.
0
 
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?
0
 
existenz2Commented:
I would give that a try brian.. Nothing to loose right? But your findings do support that idea.
0
 
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!

Brian
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now