I’ve been working on this for a little bit now, and it was surprisingly easy. I just didn’t know all of the syntax.
First, any element in a webpage that scrolls has a scrollTop attribute that’s equal to the number of pixels it has been scrolled. When it’s scrolled all the way to the top, scrollTop is zero.
To cause this to persist, you can store it in a hidden input.
<input type=”hidden” name=”whatever” id=”whatever” value=”default” runat=”server”>
Name and id should be equal, and making sure runat=”server” is present is important. Now this should persist automatically. The only other thing that’s needed is to store and restore the value. You can use an onscroll=”nameOfJavascriptFunctionThatStoresThePosition()” attribute in the div in question to call a function whenever the div is scrolled. Then in your Javascript section, set window.onload equal to whatever function is going to restore it. Parentheses aren’t needed at the end because you’re mapping onload to a function.
The last thing to remember is that in your storing and restoring functions, you can’t simply refer to the id of the hidden input because your codebehind might, and most likely will, change it. You’d have to use <%=idOfInput.ClientID%>. You can use document.getElementById(‘ blah ‘).value, where blah is that previous tag. To restore the value that value should be set into the div’s scrollTop (again, you can use document.getElementById(), but you won’t need to do ClientID stuff because the div’s not runat=”server”. To store the value, you just set the scrollTop into that hidden input using that same method. Pretty simple!
Of course, if you’re not concerned with a div but rather the whole document, you can do a similar thing using body’s scrollTop or just enable SmartNavigation if the page is being designed for InternetExplorer.
