I read this question and implemented the approach that has been stated regarding setting the response HTTP status code to 278 in order to avoid the browser transparently handling the redirects. Even though this worked, I was a little dissatisfied as it is a bit of a hack.
After more digging around, I ditched this approach and used JSON. In this case, all responses to AJAX requests have the status code 200 and the body of the response contains a JSON object that is constructed on the server. The JavaScript on the client can then use the JSON object to decide what it needs to do.
I had a similar problem to yours. I perform an AJAX request that has 2 possible responses: one that redirects the browser to a new page and one that replaces an existing HTML form on the current page with a new one. The jQuery code to do this looks something like:
$.ajax({
type: "POST",
url: reqUrl,
data: reqBody,
dataType: "json",
success: function(data, textStatus) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
} else {
// data.form contains the HTML for the replacement form
$("#myform").replaceWith(data.form);
}
}
});
The JSON object "data" is constructed on the server to have 2 members: data.redirect
and data.form
. I found this approach to be much better.
As you're using UpdatePanels you're going to need to hook into the ASP.NET AJAX PageRequestManager
You'll need to add a method to the endRequest event hooks that are:
Raised after an asynchronous postback is finished and control has been returned to the browser.
So you'd have something like:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageLoaded);
function pageLoaded(sender, args) {
window.scrollTo(0,0);
}
</script>
Which will force the browser to scroll back to the top of the page once an update request has completed.
There are other events you could hook into instead of course:
beginRequest // Raised before the request is sent
initializeRequest // Raised as the request is initialised (good for cancelling)
pageLoaded // Raised once the request has returned, and content is loaded
pageLoading // Raised once the request has returned, and before content is loaded
The beauty of asynchronous post-backs is that the page will maintain the scroll height without you having to set MaintainScrollPosition, as there is no "full page reload" happening, in this case you actually want that effect to happen, so you will need to manually create it.
Edit to respond to updated question
Ok, so if you need to only reset the postion on certain button presses you'll need to do something like this:
Start by hooking into the BeginRequest instead/as well:
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
This is because in the args parameter you get access to:
args.get_postBackElement().id
Which will tell you the id of the button that started the whole event - you can then either check the value here, and move the page, or store it in a variable, and query it in the end request - being aware of race conditions, etc where the user clicks another button before your original update completes.
That should get you going with any luck - there's quite a few examples around this on Working with PageRequestManager Events
Best Answer
You can just use jQuery and scrollTo: http://demos.flesler.com/jquery/scrollTo/
Or just write the animation yourself:
$('body').animate({ scrollTop: 0 }, 1000);