I have a div within an aspx page with overflow set to auto. The contents of the div are dynamically created and consists of a list of link buttons.
<div id="div1" style="overflow: auto; height: 100%;">
.....
</div>
When I scoll down in the div and click on any of the link buttons, the page reload loses the scroll position inside the div and takes me to the top of the div. Is there any way to prevent that?
Note that this is for a div inside the page. Page.MaintainScrollPositionOnPostBack()
does not work.
One way to do it is to capture, in the onscroll event of the div, the values from the scrollLeft and scrollTop properties. Save those values in a hidden text box(es). On post back, use the values from the text boxes to reset the properties.
As Jeff S mentioned one way to handle this situation is using javascript to track the scroll position of the div and each time the page loads reset the scroll position to its previous value.
Here's some sample code:
In practice I wouldn't put the javascript directly in the elements but its just an example. You could also store the scroll position in a cookie instead.
What happens when the link buttons are pressed? What processing occurs during the postback?
Depending on the answers to those questions, you may want to investigate getting rid of the postback altogether and performing the necessary operations purely client-side.
(I am currently performing this kind of conversion for a client.)
Usage:
Since some people don't want to use an Updatepanel for the sole purpose of saving scroll position... :)
The easiest way is to wrap the control in an UpdatePanel.