Page 1 of 1

How to maintain scroll position on Postback

Posted: Thu May 19, 2011 11:39 am
by AbleMods
If you've ever spent hours writing a page only to have it jump all over the place during any postback, this tip will make your day. With one simple command in Page_Load() you can make the page stay exactly where it was before the postback fired.

In your Page_Load() routine, add this line:

Code: Select all

this.Page.MaintainScrollPositionOnPostBack = true;
Now some will say "I can already do that in the .ASPX file." That's swell, but in AbleCommerce-Land you rarely work with the original .ASPX file. In AbleCommerce-Land the vast majority of your work will be inside User Controls, so you don't have direct access to the page declaration.

Using this technique works great in a user control and you don't have to worry about modifications to a .ASPX file conflicting with other controls that use the same .ASPX file.

Nifty :wink:

Re: How to maintain scroll position on Postback

Posted: Thu Dec 15, 2011 5:45 pm
by jonw2m
I know this is an older post - probably for an older version of AC.
I try with Able 7.0.7 - and it won't work. From what I read is because of AJAX?

Thank you,

Re: How to maintain scroll position on Postback

Posted: Fri Dec 16, 2011 2:34 pm
by jonw2m
For some reasons nothing I found here worked for me.
I made a new page in admin with lots of fields and I put this line:

ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#MOVEHERE';", true);

under
if (!Page.IsPostBack)
in your .cs file

and put the anchor:
<a name="MOVEHERE"></a>

at the top of the screen where you want to move after the postback.

Re: How to maintain scroll position on Postback

Posted: Fri Feb 24, 2012 8:54 pm
by AbleMods
When I'm working in an ASPX page, I'll usually do it in the page declaration with the MaintainScrollPositionOnPostback parameter:

Code: Select all

<%@ Page Language="C#" MasterPageFile="~/Admin/Admin.master" MaintainScrollPositionOnPostback="true"  AutoEventWireup="true" CodeFile="CouponProducts.aspx.cs" Inherits="Admin_Marketing_Coupons_CouponProducts" Title="Products Linked to Coupon" %>
I'm not sure how it would work if the postback occurred inside an AJAX UpdatePanel...a lot of the page behavior changes when those update panels get involved.