MaintainScrollPositionOnPostback Property - I love it.

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.

21 Comments

  • 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

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

  • Does it work in Mozilla/Firefox?

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



    Chill...

  • NO sirve con AJAX :(

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

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

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

    this.Page.MaintainScrollPositionOnPostBack = true;

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

  • 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

  • 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?

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

  • 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!

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

  • Thanks. It works greate.

  • 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?

  • 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?

  • thank u so much ...

  • Great Article ..Help me alot..:)

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

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

Comments have been disabled for this content.