How to reset scroll position after an UpdatePanel post back?

johna by | June 6, 2014 | ASP.NET Web Forms Web Development

If you use ASP.NET UpdatePanels, most of the time you want to maintain the window scroll position but there are occasions when you want the page to scroll back to the top or to a specific control. For example, when you have a paging control you should return the user to the top of the list.

It is quite simple to accomplish this using the ScriptManager's registerStartupScript method.

To reset the scroll position (back to top), add the following code to your postback event:

ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "ScrollTop", "setTimeout('window.scrollTo(0,0)', 0);", true);

If you use jQuery you can use the following code to scroll to the top of a specific element (with the ID mydiv in this example), no plugin required:

ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "ScrollTop", "setTimeout(\"$('html, body').scrollTop($('#mydiv').offset().top)\", 0);", true);

Using jQuery opens up many other options, including animating the scroll position.

NB. In the above examples change UpdatePanel1 to the ID of your UpdatePanel.

If this tip helped you please share on Facebook, Twitter or Google+.

Related Posts

Web Development Retro Computing

Converting dBase IV programs to run in the browser

by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Comments

David

by David | November 2, 2016

Very useful - thanks

Reply

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.