How to keep whole page scroll position after asynchronous postback

前端 未结 3 775
再見小時候
再見小時候 2021-01-16 13:47

i am using asp.net 4.0 iis 7.5 microsoft visual studio 2010

what i want is keep whole page (browser) scroll position (not a div or panel) when asynchronous postback

相关标签:
3条回答
  • 2021-01-16 14:00

    I found it here :http://forums.asp.net/t/1300961.aspx

    just add it right after script manager. it works for me in all browsers

    <script type="text/javascript">
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(beginRequest);
        function beginRequest()
        {
            prm._scrollPosition = null;
        }
    </script>
    
    0 讨论(0)
  • 2021-01-16 14:04

    asp.net has @page directive property called MaintainScrollPositionOnPostBack

    hope this will help

    0 讨论(0)
  • 2021-01-16 14:18

    You can do it the client way :

    $(document).ready(function () {
        $(window).on('beforeunload', function () {
            document.cookie = "keepscroll=" + $(window).scrollTop();
        });
        var cs = document.cookie ? document.cookie.split(';') : [];
        var i = 0, cslen = cs.length;
        for (; i < cs.length; i++) {
            var c = cs[i].split('=');
            if (c[0].trim() == "keepscroll") {
                $(window).scrollTop(parseInt(c[1]));
                break;
            }
        }
    });
    

    If you are not jQuery's friend then you could try something like :

    window.onbeforeunload = function () {
        document.cookie = "keepscroll=" + document.body.scrollTop;
    };
    var keepscroll = window.setTimeout(function () {
        var cs = document.cookie ? document.cookie.split(';') : [];
        var i = 0, cslen = cs.length;
        for (; i < cs.length; i++) {
            var c = cs[i].split('=');
            if (c[0].trim() == "keepscroll") {
                window.scrollTo(0, parseInt(c[1]));
                break;
            }
        }
        window.clearTimeout(keepscroll);
        keepscroll = null;
    }, 100);
    
    0 讨论(0)
提交回复
热议问题