ASP.net Postback - Scroll to Specific Position

后端 未结 9 1083
天命终不由人
天命终不由人 2020-12-15 16:50

I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page

9条回答
  •  难免孤独
    2020-12-15 17:07

    In your case I suggest you to keep the default value of Page.MaintainScrollPositionOnPostBack, and use the pure javascript scrolling function

    function scrollToDiv()
    {
        document.getElementById('yourDiv').scrollIntoView();
    }
    

    And call it at the page startup with a little delay of 1ms (pure javascript again)

    setTimeout(scrollToDiv, 1);
    

    And finally call it from the C# code behind, with the RegisterStartupScript (js executed after all the page has been loaded) :

    ScriptManager.RegisterStartupScript(Page, typeof(Page), "ScrollToADiv", "setTimeout(scrollToDiv, 1);", true);
    

    Like this, it will bypass any asp automatic scrolling

提交回复
热议问题