简体   繁体   English

如何避免每次回发后页面跳动?

[英]How do I keep my page from jumping around after every postback?

I'm using an UpdatePanel and the form auto-saves after every onTextChanged trigger. 我正在使用UpdatePanel,并且在每个onTextChanged触发器之后,表单都会自动保存。 However, the page keeps going to the top. 但是,页面一直到顶部。 How do i keep the page where the onfocus is? 如何将页面保持在焦点所在的位置?

I'm currently using the MaintainScrollPositionOnPostback="true" but that's not working: 我目前正在使用MaintainScrollPositionOnPostback="true"但无法正常工作:

<%@ Page Title="Welcome" Language="C#" MasterPageFile="account-master.master" 
        AutoEventWireup="true" 
        CodeFile="Welcome.aspx.cs" 
        Inherits="Welcome"  
        MaintainScrollPositionOnPostback="true" 
    %>

I used the following javascript code to fix this issue: 我使用以下javascript代码解决了此问题:

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

This seemed to work. 这似乎有效。 runs on windowload 在windowload上运行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM