MaintainScrollPositionOnPostback Property - I love it.

Published Friday, February 25, 2005 3:51 PM

With all the new features in .Net V2, its easy to miss things. You get busy looking at some of the more 'meaty' aspects, that you miss the small things. I have been enjoying and playing with lots of new ASP.Net V2 features such as the security controls, web parts. client callbacks etc.. that I never took the time to appreciate the ease and simplicity of a property in the System.Web.UI.Page object called 'MaintainScrollPositionOnPostback'. Sure, there is probably many of you out there who have already enjoyed the ease and simplicity of this boolean property, but its things like this that I really enjoy seeing.

Simple, works great, saves you time, and just shows that the designers of V2 have really done some homework from an ease of use and common usage scenario perspective.

Basically, just set this proeperty to true (eg. Page.MaintainScrollPositionOnPostback = true;) and javascript will be inserted into your rendered page, that maintains the scroll position in the browser window for all postbacks. Those of us who have done this manually, know that its not rocket science to achieve, but its sooooo nice to be able to set a single page property to just 'make it happen'.

As a final nice touch, if it could be a property in designer view (or am I blind?), that would be great.

by Glav

Comments

# Greg Low said on Friday, February 25, 2005 1:56 AM

Hi Paul,

I think it's a competition to get even with the "typing challenged" where framework class developers try to outdo each other with long names. I particularly also like the new:

FireInfoMessageEventOnUserErrors property on the SQLConnection object !

Regards,

Greg

# James said on Friday, February 25, 2005 8:41 AM

Is autocomplete broken on your VS.net install Greg? I'd just type in 'main', hit ctrl + space and select the property.

# Daniel Auger said on Friday, February 25, 2005 3:10 PM

Does it work in Mozilla/Firefox?

# Amrit said on Sunday, October 15, 2006 11:54 PM

Good property, if want in all pages in one go, simply include it in as Chill...

# victoria said on Friday, November 3, 2006 4:42 PM

NO sirve con AJAX :(

# vijay said on Friday, December 8, 2006 12:41 AM

How to maintain the scroll in UserControls(.ascx), when I place the control in .aspx page.  I have given the property true in user control and even aspx page also. But it unable to maintain for control level.

# Glav said on Sunday, December 10, 2006 2:13 AM

Vijay,

A separate control will not be affected by this property. When this property is true, Javascript is injected that specifically targets the page. You would need to do the same for a specific control if thats what you need. Look at the Javascript that is injected into the page when this property is true, for an idea of how to do this.

# mrhaze said on Monday, April 16, 2007 10:46 AM

How about just setting it in the Load event on the user control?

this.Page.MaintainScrollPositionOnPostBack = true;

# Paul said on Monday, June 4, 2007 5:37 AM

This is great, but is it possible to make it work with a panel within a page? I have a panel on my page which is set to be 400px in height. this panel has an overflow set so scrollbars will appear should this length be exceded. Within this panel I have a number of radiobuttonlists that fire an even when changed. The trouble I am having is that each time the even fires, it returns to the top of the panel. Using thie MaintainScrollPositiononPostback works prefectly for the page scrollbar, but I am trying to eliminate the need to scroll the page at all.

# alok said on Friday, March 7, 2008 8:13 AM

great idea MRHAZE

i was cofused how to do it in dotnetnuke module

as i am using usercontroles as modules in it

cool bro

nice thought

# Tony Bullock said on Monday, April 21, 2008 10:27 AM

I've been using MaintainScrollPositionOnPostBack for a while now, but frustratingly it doesn't work on Windows Mobile.  I have some Street Wardens filling in forms remotely on Windows Mobile device, but the page has quite a few buttons that fire off post backs.  They still have to scroll down the page each time they press a button that reloads the page.  :-(

Or am I missing something?

# Glav said on Thursday, April 24, 2008 1:07 AM

Tony,

Its not suprising that it does not work in Windows Mobile. The JS support is not that great on those devices and I suspect you may have to write something customised to suit whatever Windows Mobile OS and browser combination you are doing.

Not that I have actually tested it on a mobile device tho.

# jpitt57488 said on Thursday, May 15, 2008 3:53 PM

I too have found that this does not work in Mobile Web Forms.  It is very frustrating how so many things don't work in mobile web forms (such as setfocus).   You have to spend a great deal of extra time with custom java script if you want a user-friendly interface.

Mobile support is actually built into all asp.net 2.0 controls.  Thus, you can actually just build regular asp.net pages and they will work.  However, I found that the pages are much larger than the mobile pages thus they take a long time to load on a mobile device.  That is probably why microsoft chose to keep the Mobile Web forms.

Hopefully they will get this right one day!  

# jpitt57488 said on Friday, May 16, 2008 6:24 PM

I have to edit my previous comment...I did actually get this to work in a mobile form.  It did not seem to work when I added as a page directive; however when I added in code in the page_init event

Page.MaintainScrollPositionOnPostBack = True

It worked!  

One small note...still doesn't work in firefox.

# rockzcm said on Wednesday, June 25, 2008 7:30 PM

Thanks. It works greate.

# Senthrax said on Wednesday, September 10, 2008 3:58 AM

I am trying to use this in a custom user control aswell (dotnetnuke module) but it just won't work.

I do see the javascript at the bottom of the page when I view source. Have tested IE7 and FF3. both unsuccessfull

Any ideas?

# Kaushal said on Thursday, June 4, 2009 3:47 AM

Works like a charm  !! Thank you.

# Aaron said on Thursday, August 6, 2009 12:57 PM

Great...but this ONLY WORKS in IE...which is very sad. Are there any ways to make this work for any other browser than just IE?

# rupesh bari said on Monday, January 11, 2010 7:00 AM

thank u so much ...

# Mitesh said on Thursday, January 21, 2010 1:15 AM

Great Article ..Help me alot..:)

# Raveesh.R.P said on Wednesday, March 31, 2010 8:14 AM

Glav..........................,

Great...simply great......helps me a lot, on coding...

# lennyrawles said on Thursday, August 12, 2010 2:29 AM

Nice.  Time saved.  I linked back here just because of that.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication