By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,923 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Reset scroll position

P: 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
Feb 13 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <cm*@nospam.comwrote in message
news:uB**************@TK2MSFTNGP03.phx.gbl...
>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

Feb 14 '07 #2

P: n/a
Hi,

C. Moya wrote:
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
--
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
Feb 17 '07 #3

P: n/a
"Laurent Bugnion [MVP]" <ga*********@bluewin.chwrote in message
news:OD**************@TK2MSFTNGP04.phx.gbl...
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
Feb 18 '07 #4

P: n/a
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
Apr 2 '07 #5

P: n/a
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
Apr 2 '07 #6

P: n/a
Nice!

--
-C. Moya
www.cmoya.com
<guinnesswrote in message news:20***************@tiscali.co.uk...
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

Apr 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.