Preserve page scroll position after a Web Forms postback

Web Forms can emit script that restores the page's scroll position after a full postback. Enable the behavior on the content page rather than trying to treat a master page as the request's page.

This is a maintenance setting for .NET Framework Web Forms. It does not control scrolling inside every independently scrollable element.

Test the page directive

Add ScrollDemo.aspx to an existing Web Forms application:

HTML
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" %>
<!doctype html>
<html><body>
<form id="MainForm" runat="server">
  <p>Scroll down to the button.</p>
  <div style="height:1200px"></div>
  <asp:Button ID="Refresh" runat="server" Text="Post back" OnClick="Refresh_Click" />
  <asp:Label ID="Result" runat="server" />
</form>
<script runat="server">
protected void Refresh_Click(object sender, EventArgs e)
{
    Result.Text = DateTime.UtcNow.ToString("O");
}
</script>
</body></html>

Open the page, scroll to the button, and click it. The timestamp changes and the browser should return to approximately the same vertical position.

Verify the real layout

The feature depends on browser script and the resulting page dimensions. If content above the saved position changes height, the same pixel position may no longer identify the same content. Separate scroll containers and asynchronous UpdatePanel updates can need their own state handling.

When navigating to another page or moving focus to a validation error, restoring the old position may be the wrong behavior. Use the page setting only where retaining context benefits the interaction, and test keyboard focus as well as the visible scroll position.

Official references

The ASP.NET 2.0 Page Model
In ASP.NET 1.x, developers had a choice between an inline code model and a code-behind code model. Code-behind could be implemented using either the Src attr...
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/moving-to-aspnet-20/the-asp-net-2-0-page-model
ASP.NET page model

Author: Herman Schoenfeld