How to maintain scroll position on Postback

This forum is where we'll mirror posts that are of value to the community so they may be more easily found.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

How to maintain scroll position on Postback

Post by AbleMods » Thu May 19, 2011 11:39 am

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:
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

jonw2m
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 35
Joined: Thu Sep 15, 2011 9:00 pm

Re: How to maintain scroll position on Postback

Post by jonw2m » Thu Dec 15, 2011 5:45 pm

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,

jonw2m
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 35
Joined: Thu Sep 15, 2011 9:00 pm

Re: How to maintain scroll position on Postback

Post by jonw2m » Fri Dec 16, 2011 2:34 pm

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.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: How to maintain scroll position on Postback

Post by AbleMods » Fri Feb 24, 2012 8:54 pm

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.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

Post Reply