Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain scroll position on autopostback?

How can I get back to the same position of a page on postback. It always seems to get to the top of the page.

I've tried using maintainScrollPositionOnPostBack = "true"

But its not working.

like image 426
BobLoblaw Avatar asked Feb 28 '11 21:02

BobLoblaw


People also ask

How do you maintain the scroll position?

HTML's div with the vertical scrollbar gets back to the original position after PostBack and hence to solve this problem, we need to retain its scroll position value, using JavaScript and then again apply to div after PostBack. This technique helps DIV not to lose its scroll position on PostBack.

How can I retain the scroll position of a scrollable area when pressing back button?

During page unload, get the scroll position and store it in local storage. Then during page load, check local storage and set that scroll position.

What is MaintainScrollPositionOnPostBack?

On long Web pages, this means that the user has to scroll the page back to the last position on the page. When the MaintainScrollPositionOnPostBack property is set to true , the user is instead returned to the last position on the page. You set the MaintainScrollPositionOnPostBack property in the @ Page directive.


2 Answers

I've recently looked for this as well. Came up with a load of Javascript to be inserted until I found the following:

At the top of your .aspx codefile, insert the following:

 MaintainScrollPositionOnPostback="true"

so the very first sentence in your .aspx starts

<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs"

This works just fine for me without having to add any other code for keeping scrollbar positions using updatepanels

like image 132
bbbwex Avatar answered Sep 28 '22 21:09

bbbwex


There are a few ways I have used to set maintainScrollPositionOnPostBack. Have you tried more than one? Can you describe what is triggering the postback and which browsers you have tested? Are you using a master page?

  1. You can set Page.MaintainScrollPositionOnPostBack = true; in the code behind on page load.
  2. You can add it to the page declaration <%@ Page MaintainScrollPositionOnPostback="true" %>
  3. You can add it in the web config file <pages maintainScrollPositionOnPostBack="true" />
like image 40
K Richard Avatar answered Sep 28 '22 23:09

K Richard