Dave Knipper Was Here

Conditionally Reset Scroll Position on a Partial Postback

Here’s how to conditionally reset the browser’s scroll position of an ASP.NET page after an AJAX partial postback. You will of course need to have your web application ASP.NET Ajax ready.

Here’s the code. The following constant is the key that is sent by the server to the client browser after a postback. If this key is found by the client, the JavaScript function will tell the browser to reset the scroll position. Here’s the page load event handler. In this event, you need to generate some JavaScript that contains the logic that tells the client browser how to reset the browser’s scroll position. This code basically consumes the data sent from the server, determines what data item was registered for the call along with its value. If that value contains the key constant, the browser will reset its scroll position. So... how do we send the key to tell the browser to reset the scroll position? First we need to initiate a partial postback (in this case it’s a button click) When a partial postback is initiated, a server side event is invoked and handled. This event handler checks a condition, and if the condition is true, the ASP.NET Ajax Data Item (in this case the page itself) gets sent back to client with the KEY value. In order for this to work, the control causing the partial postback event needs to be inside an update panel on the .aspx page in order for this work.

Here’s the code containing the event handler: Here’s the markup code for the button instantiating the partial postback. This code can also serve as a base for anyone who wants to send statements back to a JavaScript function conditionally.