What is the best way to reset the scroll position to the top of page after the an asynchronous postback?
The asynchronous postback is initiated from a ASP.NET GridView CommandField column and the ASP.NET update panel Update method is called in the GridView OnRowCommand.
My current application is an ASP.NET 3.5 web site.
EDIT: I have received excellent feedback from everyone, and I ended using the PageRequestManager method in a script tag, but my next question is:
How do I configure it to only execute when the user clicks the ASP.NET CommandField in my GridView control? I have other buttons on the page that perform an asynchronous postback that I do not want to scroll to the top of the page.
EDIT 1: I have developed a solution where I do not need to use the PageRequestManager. See my follow up answer for solution.
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:
So you'd have something like:
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:
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:
This is because in the args parameter you get access to:
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
Here is the perfect solution to reset scroll bar position to TOP in AJAX
Client Side Code
Server Side Code
Only,window.scrollTo(0,0) will not work. Ajax will take precedence in this case so you have to use setTimeout function with that.
Are you using asp.net AJAX? If so there are two very useful events in the client libraries beginRequest and endRequest, one of the problems with partial/async postbacks, is that the general page hasnt got a clue what you are doing. the begin and endrequest events will allow you to maintain scroll position at the client, you could have a var in js that is set to the scroll position on beginrequest, then on end request you can reset whichever element's scrollTop you require.. i'm sure ive seen this done before but cant find a link. ill post if i find an example
Put the above to the function that you want to tell to go to the top of page. Like when the page is not valid, have that there and it will add temporary javascript to shoot you to the top of the page.
taken from this tutorial:
http://aspnet.4guysfromrolla.com/articles/111407-1.aspx
We set MaintainScrollPositionOnPostback=true
Then if we need to reset scroll position call the method:
Have you set the page property MaintainScrollPosition? Not sure if that would be any different if you do an Async Postback or not.
Edit: One thing you could attempt is to set focus on a particular item near the top of your page, that may help and be a dirty work around.