“maintainScrollPositionOnPostBack=”true“ ” does not work with google chrome

16,448

Solution 1

You can add this snippet to your ASP.NET Page/MasterPage (jQuery required):

<asp:HiddenField runat="server" ID="hfPosition" Value="" />
<script type="text/javascript">
    $(function () {
        var f = $("#<%=hfPosition.ClientID%>");
        window.onload = function () {
            var position = parseInt(f.val());
            if (!isNaN(position)) {
                $(window).scrollTop(position);
            }
        };
        window.onscroll = function () {
            var position = $(window).scrollTop();
            f.val(position);
        };
    });
</script>

Solution 2

I also faced same problem. I found one Javascript solution here.

<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;
    }
};

I hope this would help you.

Solution 3

I couldn't get MaintainScrollPositionOnPostback to work for me no matter what I tried. Based on Darkseal's answer and Eirik H's comment, I tried the following code which worked for me. This will only work if you have an ASP.NET ScriptManager (i.e. MicrosoftAjax.js) on your page. You also need JQuery added to your page. Add the below code to your .aspx file somewhere underneath asp:ScriptManager tag.

<asp:HiddenField runat="server" ID="hfPosition" Value="" />
<script type="text/javascript">
    $(function () {
        var positionField = $("#<%=hfPosition.ClientID%>");
        window.onscroll = function () {
            var position = $(window).scrollTop();
            positionField.val(position);
        };
    });

    function pageLoad() {
        var positionField = $("#<%=hfPosition.ClientID%>");
        var position = parseInt(positionField.val());
        if (!isNaN(position)) {
            $(window).scrollTop(position);
        }
    };
</script>

Basically we are holding the scroll position inside the value of a hidden field called hfPosition. Whenever the page is scrolled, the value will be updated. Then when a postback happens pageLoad() will automatically be called and will get the value of hfPosition and scroll to that value.

Including the ScriptManager and JQuery, my final code snippet looked something like this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script src="../Scripts/jquery-3.3.1.min.js" type="text/javascript"></script>
<asp:HiddenField runat="server" ID="hfPosition" Value="" />
<script type="text/javascript">
    $(function () {
        var positionField = $("#<%=hfPosition.ClientID%>");
        window.onscroll = function () {
            var position = $(window).scrollTop();
            positionField.val(position);
        };
    });

    function pageLoad() {
        var positionField = $("#<%=hfPosition.ClientID%>");
        var position = parseInt(positionField.val());
        if (!isNaN(position)) {
            $(window).scrollTop(position);
        }
    };
</script>/>
Share:
16,448
Pranav Kumar
Author by

Pranav Kumar

Updated on June 18, 2022

Comments

  • Pranav Kumar
    Pranav Kumar almost 2 years
    1. Web.config Level => pages maintainScrollPositionOnPostBack="true" />

    2. Page Level => <%@ Page MaintainScrollPositionOnPostback="true" %>

    3. Code Level => Page.MaintainScrollPositionOnPostBack = true;

    4. Browser Level => browser id="Chrome" parentID="Safari1Plus"> capabilities> capability name="supportsMaintainScrollPositionOnPostback" value="true" /> capabilities> browser>

    Any of the 4 ways mentioned above did not work with google chrome. It is working fine with firefox. Kindly provide any solution .

  • Eirik H
    Eirik H over 8 years
    For anyone coming here for a solution to this with an ajax call, window.onload won't run so the scrolling never happens. Instead I suggest to use the special asp.net function pageLoad. function pageLoad() {var position = parseInt(f.val());if (!isNaN(position)) {$(window).scrollTop(position);}}