|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionBy default, the scroll position of The code has been tested with IE6, IE7, FF 2.0.0.12, and Opera 9.26, and should basically work with any browser supporting the BackgroundI recently came across the prescribed problem above. So, I Googled and found an article on CodeProject (Persisting the scroll position of child DIV’s using MS AJAX) - however, the provided solution didn't work for me, so I decided to implement my own solution. Using the codeAdd the following code between your <script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
setScrollPos();
}
function saveScrollPos(){
document.getElementById("scrollPos").value =
document.getElementById("divScroll").scrollTop;
}
function setScrollPos(){
document.getElementById("divScroll").scrollTop =
document.getElementById("scrollPos").value;
}
</script>
NB! You might have to change the public static string scrollPos = String.Empty;
protected void Page_Load(object sender, EventArgs e){
scrollPos = ((HtmlInputHidden)scrollPos).ClientID.ToString();
}
Then, change the JavaScript to: <script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
setScrollPos();
}
function saveScrollPos(){
document.getElementById("<%=scrollPos%>").value =
document.getElementById("divScroll").scrollTop;
}
function setScrollPos(){
document.getElementById("divScroll").scrollTop =
document.getElementById("<%=scrollPos%>").value;
}
</script>
Assuming that you have already added a <input type="hidden" id="scrollPos" name="scrollPos" value="0" runat="server"/>
<asp:UpdatePanel runat="server" ID="up1" UpdateMode="always">
<ContentTemplate>
<asp:Button runat="server" ID="button1" text="Post back!" />
<div id="divScroll" onscroll="saveScrollPos();"
style="height: 200px; overflow:auto; overflow-x:hidden; overflow-y:scroll;" >
</div>
</ContentTemplate>
</asp:UpdatePanel>
Points of interestThe trick that made the code work compared to other resources I have looked at was the Updates
|
|||||||||||||||||||||||||||||||||||||||||||||||||||