Reset scroll position

C

C. Moya

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

C. Moya

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).
 
L

Laurent Bugnion [MVP]

Hi,

C. Moya said:
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.
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
 
C

C. Moya

Laurent Bugnion said:
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.
 
G

guinness

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
 
G

guinness

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.ClientScript.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top