Saturday, March 24, 2012

Maintaining Scroll Position on Redirect

Hi,

In ASP.NET 2.0 it is very easy to maintain the scroll posotion on postbacks. How can we do this on page redirects?

KaaNIf you redirect to another page, you can't use the maintain position feature, what you have to do, is to pass the y position of the scroller to the page your redirect to. You can after that with client-side script set the scroller position (you can use the onLoad event of the <BODY> element to trigger the client side method).

Here is an example of a method that will set the y scroller to a specified position.

<code>
function setPosition(position)
{
document.body.scrollTop = position;
}
<code>
Hi,

I can read the Y scroll position with Request.Params("__SCROLLPOSITIONY").
I tried to set this hiddenfield like that:

RegisterHiddenField("__SCROLLPOSITIONY", 321)

but it didn't work. I get error by this function: Page.LoadScrollPosition()

This must be a private function of the Page. It would be nice if we could override it someway.

KaaN
And I hate Javascript.
If you use the ResisterHidenField, you will add new fields to the page, so in your case you will have two fileds with the same name if the maintain position feature is enabled.

What you can do is to create your own unique hidden field instead, or use client-side script that will set the __SCROLLPOSITIONY field to your value. If you don't turn on the maintain position for the page you redirect to, you can register the __SCROLLPOSITIONY field. But in your case you can simply register a client-side script that will automatically set the position for the scroller when the page is loaded (you can use the script I have provide you with in my previous post to set the y position).

The LoadScrollPoisition is an internal method that will parse and return the value located within the __SCROLLPOSITIONY field.
By the way, why do you want to set the scroll position on a page that you redirect to? If the scroll position on the page you redirect from is at the bottom of the page or middle etc, the page you will redirect to will have the scroller on the same position, but if you redirect to a new page wouldn't the user want to read the page from the top!?
>And I hate Javascript.

hehe, the maintain position feature will use client-side script, but will add it behind the scene ;)

The only way to set the positon of the scroller, is to use client-side script.
My problem is the opposite. I've got a large system programmed bysomeone else (a DotNetNuke installation with some custom-programmedmodules) and I need to extend them. Generally, the pages alreadyremember their scroll position, I haven't the faintest idea how it isdone, and this is the right behaviour in most cases. There is one casethough in which I'd like the reloaded page to scroll to the top:DotNetNuke usually displays error messages on top of the page, andsometimes users with a smaller screen resolution just can't see thembecause they're off their screen.
I've toyed with the JavaScript and window.scrollTo and window.scrollBy,and my problem is how to make sure the functions are called when thepage gets reloaded. I only want it to reposition itself in special cases.
Can you help me?
OK, reading helps a lot sometimes... Sorry I asked this unnecessary question when the answer was contained in the previous posts. The hint about the onLoad attribute of the tag helped though. So what I do in my code is to programmatically set the value of the onLoad attribute in my code iff I want to reposition. Great.

Thanks!

dogcow (moof!)

0 comments:

Post a Comment