Tip #75: Did you know…How to maintain scrollposition after post back?


When web pages are posted back to the server, by default user is returned to the top of the page. On a large web page, you might have a requirement to scroll down the user automatically to the last position on the page.

MaintainScrollPositionOnPostBack page property can be used to achieve this  in one of the following ways. 

  1. Application level: To set the property by default for all pages in the website, open web.config and add the attribute to the pages node.

    <pages maintainScrollPositionOnPostBack=”true”>

  2. Page Level: for a particular page, open the aspx and set the property

    <%@ Page MaintainScrollPositionOnPostback=”true” …

  3. Code level: to set the property programmatically

    Page.MaintainScrollPositionOnPostBack = true;

Hope this helps.

Deepak Verma
SDET | Visual Web Developer

Comments (12)

  1. What's New says:

    When web pages are posted back to the server, by default user is returned to the top of the page. On

  2. Rajeesh says:

    Is this supported in all browsers? I know that it will work in IE and FF, what about safari?

    Ta,

    Rajeesh

  3. Very veyr helpful tips.

    Thanks,

    Thani

  4. Kunal - "the BOSS" says:

        Its so simple , but i just know today only having 2+ year of experience.

    and i am searching for this kind of stuff.

    sometimes this little code makes lots of help.

    i dont believe that it is so simple to maintain scroll position after post back.

    thanks.

  5. Alireza says:

    Thanks for this remarkable tip!

  6. santosh says:

    Yes, this is little code but so important

  7. Greg says:

    This is a good example why we do simple web page based applications now.  The older heavyweight GUI .NET 2.0 era applications we developed are much harder and costly to maintain given that we made the mistake of attempting to mimic a desktop winforms application in ASP.NET web pages.  Our web apps are lightweight GUI pages and we use silverlight for our heavyweight winforms like GUI applications.  

    Our business users/analysts are used to MS Word interactivity and try to get it into our web applications without considering the greatly increased cost and failure risk that level of interacvtivity brings to an ASP.net application.

  8. Hi Rajeesh,

    Thanks for you feedback.

    >>>>>>>>>>>>>> I know that it will work in IE and FF, what about safari?

    A workaround for Safari can be found at http://forums.asp.net/p/1094179/1651390.aspx .

    Thanks,

    Deepak

  9. sun.Lei says:

    在ASP.NET中PostBack后保持页面的位置(滚动条位置),在内容多的页面中非常有用(长页面).

  10. KidYang says:

    Whenwebpagesarepostedbacktotheserver,bydefaultuserisreturnedtothetopofthepage.On…

  11. Venki says:

    Hi,

      All the above solution is working only for VS2008. How i can do the same in VS2010?

  12. Hi, Venki, I tried with asp.net 4.0 and asp.net 4.5, both works fine for me.  This is run time feature which works with asp.net 2.0, 3.5, 4.0 and 4.5.  So it doesn't matter with which version of Visual Studio.

    You can post your code in the asp.net forum and ask the experts to see what's wrong with your code.

    Thanks.

    Xinyang Qiu

    Web Platform and Tools team