Connecting Tech Pros Worldwide Help | Site Map

Reset scroll position

  #1  
Old February 13th, 2007, 03:55 AM
C. Moya
Guest
 
Posts: n/a
I hope someone has an answer: MaintainScrollPositionOnPostback works great.
But, the problem is that sometimes we need to manually reset the scroll
position back to the top (such as when hiding a panel and displaying
different panel).

1) Temporarily setting MaintainScrollPositionOnPostbox=False in the postback
event *seems* to work... but it's not the right answer as it messes up any
subsequent event postbacks.
2) Registering a startup script (window.scrollTo(0,0);) doesn't work because
ASP.NET puts its own scroll logic after it.

Basically we need a way to do something like Page.SetScroll(0,0) to manually
override the values saved by the MaintainScrollPositionOnPostback feature.
Any ideas?

--
-C. Moya
www.cmoya.com


  #2  
Old February 14th, 2007, 06:45 PM
C. Moya
Guest
 
Posts: n/a

re: Reset scroll position


Wow. No takers? I didn't think there would be here. But, this is a pretty
common scenerio. No one is designing pages that actually have enough content
to scroll?

Anyway, I've just resorted to manually turning off
MaintainScrollPositionOnPostback in the event when I need the page to scroll
to the top (ASP.NET automatically turns it on in the next postbox). This
achieves the effect of scrolling to the top at the expense of the very next
postback doing the same (which is not ideal).

--
-C. Moya
www.cmoya.com
"C. Moya" <cmm@nospam.comwrote in message
news:uBsVeFyTHHA.3440@TK2MSFTNGP03.phx.gbl...
Quote:
>I hope someone has an answer: MaintainScrollPositionOnPostback works great.
>But, the problem is that sometimes we need to manually reset the scroll
>position back to the top (such as when hiding a panel and displaying
>different panel).
>
1) Temporarily setting MaintainScrollPositionOnPostbox=False in the
postback event *seems* to work... but it's not the right answer as it
messes up any subsequent event postbacks.
2) Registering a startup script (window.scrollTo(0,0);) doesn't work
because ASP.NET puts its own scroll logic after it.
>
Basically we need a way to do something like Page.SetScroll(0,0) to
manually override the values saved by the MaintainScrollPositionOnPostback
feature. Any ideas?
>
--
-C. Moya
www.cmoya.com
>

  #3  
Old February 17th, 2007, 09:45 PM
Laurent Bugnion [MVP]
Guest
 
Posts: n/a

re: Reset scroll position


Hi,

C. Moya wrote:
Quote:
I hope someone has an answer: MaintainScrollPositionOnPostback works great.
But, the problem is that sometimes we need to manually reset the scroll
position back to the top (such as when hiding a panel and displaying
different panel).
>
1) Temporarily setting MaintainScrollPositionOnPostbox=False in the postback
event *seems* to work... but it's not the right answer as it messes up any
subsequent event postbacks.
2) Registering a startup script (window.scrollTo(0,0);) doesn't work because
ASP.NET puts its own scroll logic after it.
Startup scripts are added to the bottom of the page. The body.onload
event (JavaScript) will occur even after that. So I think that you
should try and add the "window.scrollTo" code in the body onload event.

To do this, you can add runat="server" to the body tag, give it an ID,
and then in the code behind use the "Attributes" collection. I didn't
test but I think it should work.
Quote:
Basically we need a way to do something like Page.SetScroll(0,0) to manually
override the values saved by the MaintainScrollPositionOnPostback feature.
Any ideas?
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
  #4  
Old February 18th, 2007, 11:45 PM
C. Moya
Guest
 
Posts: n/a

re: Reset scroll position


"Laurent Bugnion [MVP]" <galasoft-lb@bluewin.chwrote in message
news:ODdzJstUHHA.3652@TK2MSFTNGP04.phx.gbl...
Quote:
Startup scripts are added to the bottom of the page. The body.onload event
(JavaScript) will occur even after that. So I think that you should try
and add the "window.scrollTo" code in the body onload event.
>
To do this, you can add runat="server" to the body tag, give it an ID, and
then in the code behind use the "Attributes" collection. I didn't test but
I think it should work.
>
Yeah, I considered that... but then I saw that the
MaintainScrollBackPosition uses it too. I haven't tried experimenting yet.

--
-C. Moya
www.cmoya.com


  #5  
Old April 2nd, 2007, 11:25 AM
guinness
Guest
 
Posts: n/a

re: Reset scroll position


hey, I'm suffering from exactly the same problem. I haven't got a proper solution yet, but I think I'm on the right track. It depends how your pages are constructed, but I have multiple user controls on a single page, each of which are made visible in turn, through a 4-step process.

In the PreRender event of each control, you can read this.Request["__EVENTTARGET"], cast it to type WebControl, and use FindControl() to determine whether it is a child of the current UserControl.

In my implementation, I can safely assume that if the event was raised from a different control, I want to reset the scroll position to 0,0 (moving between steps).

Conversely, if the event was raised from inside the same control, it's probably just a validation failure etc, for which I want to maintain the scroll position.

I dont think this can easily be integrated with MaintainScrollPositionOnPostback, so I've still got to write the JS to control the scrolling myself and include it dynamically, based on the logic above.

Just thought the concept might help you a little!?



EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
  #6  
Old April 2nd, 2007, 01:25 PM
guinness
Guest
 
Posts: n/a

re: Reset scroll position


No, scratch that - I've found a better way! :)

Include this JS function in your page:

function resetDotNetScrollPosition()
{
var scrollX = document.getElementById('__SCROLLPOSITIONX');
var scrollY = document.getElementById('__SCROLLPOSITIONY');

if(scrollX != null && scrollY != null)
{
scrollX.value = 0;
scrollY.value = 0;
}
}

then in whichever server event you need to reset the scroll position, register it as a startup script:

Page.ClientScript.RegisterStartupScript(Page.Clien tScript.GetType(), Page.ClientID, "resetDotNetScrollPosition();", true);

...it works because this startup script gets inserted *before* ASP.NET's calls to restore the scroll position, so it ends up restoring the scroll position to 0,0. Here is the generated HTML from the end of my page:
------------------------------------------------

resetDotNetScrollPosition(); << LOOK!

theForm.oldSubmit = theForm.submit;
theForm.submit = WebForm_SaveScrollPositionSubmit;

theForm.oldOnSubmit = theForm.onsubmit;
theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit;

theForm.oldOnLoad = window.onload;
window.onload = WebForm_RestoreScrollPosition;

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
  #7  
Old April 8th, 2007, 02:15 PM
C. Moya
Guest
 
Posts: n/a

re: Reset scroll position


Nice!

--
-C. Moya
www.cmoya.com
<guinnesswrote in message news:20074282327nospam@tiscali.co.uk...
Quote:
No, scratch that - I've found a better way! :)
>
Include this JS function in your page:
>
function resetDotNetScrollPosition()
{
var scrollX = document.getElementById('__SCROLLPOSITIONX');
var scrollY = document.getElementById('__SCROLLPOSITIONY');
>
if(scrollX != null && scrollY != null)
{
scrollX.value = 0;
scrollY.value = 0;
}
}
>
then in whichever server event you need to reset the scroll position,
register it as a startup script:
>
Page.ClientScript.RegisterStartupScript(Page.Clien tScript.GetType(),
Page.ClientID, "resetDotNetScrollPosition();", true);
>
..it works because this startup script gets inserted *before* ASP.NET's
calls to restore the scroll position, so it ends up restoring the scroll
position to 0,0. Here is the generated HTML from the end of my page:
------------------------------------------------
>
resetDotNetScrollPosition(); << LOOK!
>
theForm.oldSubmit = theForm.submit;
theForm.submit = WebForm_SaveScrollPositionSubmit;
>
theForm.oldOnSubmit = theForm.onsubmit;
theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit;
>
theForm.oldOnLoad = window.onload;
window.onload = WebForm_RestoreScrollPosition;
>
EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scroll Position Reset "jerks" Frinavale answers 12 April 16th, 2009 08:19 AM
Reset scroll position on AJAX postback Mark answers 1 June 27th, 2008 09:07 PM
Resetting scroll position in MaintainScrollPositionOnPostBack Chris Rathman answers 0 May 15th, 2006 09:05 PM
Panel scroll position reset on postback rmunson8 answers 3 November 19th, 2005 08:01 PM