Javascript – Scroll to top of page after async post back

asp.netasynchronouscode-behindjavascript

So I need to scroll to the top of the page after an async post back in an asp.net update panel.

The code I used was this:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestEventHandler);
function EndRequestEventHandler(sender, args)
{
    scrollTo(0,0);
}

However, I only want this to be run when I click on a certain button which causes the async postback.

How do I wire this event up in my code behind button event?

Any help would be appreacited, thanks!

Best Answer

My quest for a solution is finally over. This question was part of the help, and the rest I found here.

Had to override ASP.NET Ajax's behaviour of memorizing the scroll position:

<script type="text/javascript"> 

var manager = Sys.WebForms.PageRequestManager.getInstance(); 

manager.add_beginRequest(beginRequest); 
function beginRequest() 
{ 
    manager._scrollPosition = null; 
} 
</script>

And then use the bit of code in the answer here, on the codebehind of the page I wanted to scroll to the top:

ScriptManager.RegisterStartupScript(this, typeof(MyControl), "someText", "window.scrollTo(0, 0)", true);