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:   <pages maintainScrollPositionOnPostBack="true" />

On page level:   <%@ Page MaintainScrollPositionOnPostback="true" %>

On code-behind level:  Page.MaintainScrollPositionOnPostBack = true;

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, since it's pure HTML+JS):

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.

 

 

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. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

2 Comments on “ASP.NET - MantainScrollPositionOnPostback for Chrome and most recent browsers”

    1. The value gets updated by the javascript code every time the user scrolls up or down (“onscroll” event) and put in the hidden input field, so that it will be available upon each postback. That same value – if present – will be fetched by javascript (in the “onload” event) and then used to update the browser’s scroll position accordingly. That’s about it.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.