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.
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">
Page Level: for a particular page, open the aspx and set the property
<%@ Page MaintainScrollPositionOnPostback="true" ...
Code level: to set the property programmatically
Page.MaintainScrollPositionOnPostBack = true;
Hope this helps.
Deepak Verma
SDET | Visual Web Developer
Comments
Anonymous
June 08, 2009
When web pages are posted back to the server, by default user is returned to the top of the page. OnAnonymous
June 08, 2009
The comment has been removedAnonymous
June 08, 2009
Very veyr helpful tips. Thanks, ThaniAnonymous
June 08, 2009
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.Anonymous
June 09, 2009
Thanks for this remarkable tip!Anonymous
June 09, 2009
Yes, this is little code but so importantAnonymous
June 09, 2009
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.Anonymous
June 09, 2009
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, DeepakAnonymous
June 21, 2009
The comment has been removedAnonymous
June 21, 2009
Whenwebpagesarepostedbacktotheserver,bydefaultuserisreturnedtothetopofthepage.On...Anonymous
July 22, 2012
Hi, All the above solution is working only for VS2008. How i can do the same in VS2010?Anonymous
July 23, 2012
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