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+.
Rate this post:
Comments
by David | November 2, 2016
Very useful - thanks
Reply