ASP.NET – MantainScrollPositionOnPostback for Chrome and most recent browsers

System.Web.HttpException (0x80004005): The application is configured to issue secure cookies - Cause e soluzioni del problema

To keep (or to say it better, to recover) the page scrollbar position after a postback is definitely a feature any form-containing page should have. This used to be a trival task for any ASP.NET developer, who could just by add a simple configuration parameter to their code:

On web.config level:  

On page level:  

On code-behind level: 

Unfortunately all these methods, other than being unavailable with Razor, aren’t working anymore with any recent non-IE browser. Sure, it’s still possible to detect and configure the behavior of each one of them by using the ASP.NET Browser Definition Files accordingly, but in order to achieve better results it’s advised to adopt a centralized, versatile solution. Here’s a simple script based on the popular jQuery framework who does the trick in a cross-browser compatible way:

For WinForms:

For Razor (and also for any web page):

Needless to say, in order to make these scripts work you have to place the input element on top (asp:HiddenField for the WinForms one) inside the form element who will handle the Postback.

If you don’t like the standard HTML   you can use the helper method equivalent provided by Razor, such as:

 

 

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan