how to retain the browser scroll position after an

2019-05-04 20:31发布

问题:

When I am clicking on any button my page is re loading and page position is getting change.

first page is scrolling bottom then going on top again its moving to down side.

How could I retain the scroll position same after page refresh on any event click.

I tried

Page.MaintainScrollPositionOnPostBack = true;

on my page load but its not working.

I used ajax updatepanel after using it my browser is getting stuck, and performance is very slow.

I have one aspx page, in that i am calling 5 web user control.

Please any one help me..

How could I retain the scroll position same after page refresh on any event click.

回答1:

Did you tried declaratively setting MaintainScrollPositionOnPostBack property on your page ,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs"
    Inherits="test" MaintainScrollPositionOnPostback="true" %>


回答2:

Have look into this article Maintain Scroll Position after Asynchronous Postback

and check this thread Reset scroll position after Async postback - ASP.NET



回答3:

I used update panel as well as this script and its working well for me..

 <script>
    var xPos, yPos;
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
    function BeginRequestHandler(sender, args) {
        xPos = document.getElementById("<%=Panel4.ClientID %>").scrollLeft;
        yPos = document.getElementById("<%=Panel4.ClientID %>").scrollTop;
    }
    function EndRequestHandler(sender, args) {
        document.getElementById("<%=Panel4.ClientID %>").scrollLeft = xPos;
        document.getElementById("<%=Panel4.ClientID %>").scrollTop = yPos;
    }
 </script>