Restore scroll position of a DIV over a postback

J

JezB

There are a few references on the net about how to restore a page's scroll
position over a postback. This is a simple one which works for me:
eg. http://www.devhood.com/messages/message_view-2.aspx?thread_id=104625

My question is : can the same thing be done to restore the scroll position
of a specific DIV within a page? I have a DIV section which scrolls
independently to the page :
<div style="vertical-align: top; height:200px; width:100%; overflow:auto;">
<asp:DataGrid runat="server" ...>
...
</asp:DataGrid>
</div>
On each postback it is scrolling back to the top. Very annoying to the user
if it's a click within the scroll area that causes the postback.

Could someone tell me how? My knowledge of javascript is almost zero.
 
J

JezB

Never mind. I found a way. For the record:

<body onload="javascript:scrollTo('scrollArea')" >
<form id="Form1" method="post" runat="server">
...
<div id="scrollArea onscroll="javascript:setScroll(this);"
runat="server" style="vertical-align: top; height:200px; width:100%;
overflow:auto;">
<asp:DataGrid runat="server" ...>
...
</asp:DataGrid>
</div>
<input type="hidden" id="scrollPos" name="scrollPos" value="0"
runat="server">
...
</form>
<script language="javascript">
function setScroll(val) {
document.Form1.scrollPos.value = val.scrollTop;
}
function scrollTo(what) {
document.getElementById(what).scrollTop =
document.Form1.scrollPos.value;
}
</script>
</body>
 
J

JezB

Well, it did work partly : if the control which causes the postback is in
the scrollable DIV it does rescroll far enough to focus on it if I turn
smart navigation on, but not EXACTLY to the same scroll position (so user
sees it jump from middle to bottom, for example). Also, if the control which
caused the postback was outside the scrollable area it always scrolled
itself back to the top. Therefore I had to come up with a better solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top