ASP.Net MaintainScrollPositionOnPostback not working in Firefox and Chrome
 
Author:
Filed Under: ASP.Net  |  Issues and Exceptions
Published Date: Feb 01, 2012
Views: 1325
 

Abstract: Here Mudassar Ahmed Khan has solved the issue ASP.Net MaintainScrollPositionOnPostback Property not working in Firefox and Chrome and provided a solution that works in all major browsers Internet Explorer IE, FireFox, Google Chrome, Apple Safari and Opera.

Comments:  1

 

Here I will explain a workaround to get rid of the problem
MaintainScrollPositionOnPostback not working in Firefox and Chrome browsers
 
To tackle with this problem I have developed my own logic to maintain scroll position when PostBack occurs or when form is submitted.
 
You just need to put the script below in your page where you want to maintain scroll position or in your master page. And I have tested this script in 5 major browser and luckily it works in all.
 
<script type = "text/javascript">
    window.onload = function () {
        var scrollY = parseInt('<%=Request.Form["scrollY"] %>');             
        if (!isNaN(scrollY)) {
            window.scrollTo(0, scrollY);
        }
    };
    window.onscroll = function () {
        var scrollY = document.body.scrollTop;
        if (scrollY == 0) {
            if (window.pageYOffset) {
                scrollY = window.pageYOffset;
            }
            else {
                scrollY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
            }
        }
        if (scrollY > 0) {
            var input = document.getElementById("scrollY");
            if (input == null) {
                input = document.createElement("input");
                input.setAttribute("type", "hidden");
                input.setAttribute("id", "scrollY");
                input.setAttribute("name", "scrollY");
                document.forms[0].appendChild(input);
            }
            input.value = scrollY;
        }
    };
</script>
 
 
 
Demo
 
 
 

The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.


Downloads

MaintainScrollPositionOnPostback.zip









Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit