How to maintain scroll position on autopostback?

前端 未结 12 1610
臣服心动
臣服心动 2020-12-30 21:49

How can I get back to the same position of a page on postback. It always seems to get to the top of the page.

I\'ve tried using maintainSc

相关标签:
12条回答
  • 2020-12-30 22:19

    Although bbbwex's reply is correct, it actually took me a while to realize I need to place MaintainScrollPositionOnPostback="true" at both

    1. Top of the ASPX page.

    2. In IsPostBack

    It works after I satisfy both condition.

    0 讨论(0)
  • 2020-12-30 22:21

    Best solution for me, was to wrap the problematic controls with an update panel

    <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
    <ContentTemplate>
    dropdown controls that cause postback etc..
    </ContentTemplate>
    </asp:UpdatePanel>
    

    ofcourse, you also need the

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    

    to be present inside the main form of the site.

    0 讨论(0)
  • 2020-12-30 22:21

    Note: If you have a default control defined, it will scroll to that control on post back even if Page MaintainScrollPositionOnPostback="true"

    0 讨论(0)
  • 2020-12-30 22:23

    There are a few ways I have used to set maintainScrollPositionOnPostBack. Have you tried more than one? Can you describe what is triggering the postback and which browsers you have tested? Are you using a master page?

    1. You can set Page.MaintainScrollPositionOnPostBack = true; in the code behind on page load.
    2. You can add it to the page declaration <%@ Page MaintainScrollPositionOnPostback="true" %>
    3. You can add it in the web config file <pages maintainScrollPositionOnPostBack="true" />
    0 讨论(0)
  • 2020-12-30 22:23

    You can set .Focus() onto a specific server control when your page posts back.

    0 讨论(0)
  • 2020-12-30 22:23

    Make sure, that you do not set default button in <form id="form1" runat="server" defaultbutton="YourDefaultButton">. Remove defaultbutton="YourDefaultButton" and MaintainScrollPositionOnPostback="true" will work.

    0 讨论(0)
提交回复
热议问题