Maintain scroller position on Div after page postback (ASP.NET)

后端 未结 6 1020
执念已碎
执念已碎 2021-01-05 05:30

I have a div as such:

I contains a few hundred records and allows me to select an i

相关标签:
6条回答
  • 2021-01-05 05:45

    Tried all of the above and none of them worked satisfactorily in chrome v39, however the method used on this page worked marvellously:

    Maintain Scroll Position of DIV on PostBack in ASP.Net

    0 讨论(0)
  • 2021-01-05 05:50

    Disclaimer - not my code, but I've seen this used before:

    window.onload = function(){
        var strCook = document.cookie;
        if(strCook.indexOf("!~")!=0){
          var intS = strCook.indexOf("!~");
          var intE = strCook.indexOf("~!");
          var strPos = strCook.substring(intS+2,intE);
    
          document.getElementById("divTest").scrollTop = strPos;
          document.getElementById("divTest").scrollTop = strPos;
        }
      }
      function SetDivPosition(){
        var intY = document.getElementById("divTest").scrollTop;
    
        document.cookie = "yPos=!~" + intY + "~!";
      }
    

    The idea is to store the position of the scrollbar in a cookie. Another (better?) option would be to store it in a hidden field (or fields). Hope that gets you going...

    0 讨论(0)
  • 2021-01-05 05:51

    Replace thebody with document.getElementById("divTest")

    If you worry that the onscroll event does not work in opera/ff, you can try changing

    thebody.onscroll=SaveScrollLocation;
    

    to

    setInterval('SaveScrollLocation()", 500);
    
    0 讨论(0)
  • 2021-01-05 05:54

    Here is a more refined way of FlySwat's solution using JQuery which worked for me:

        var $ScrollPosition = $('#hfScrollPosition');
        var $ScrollingDiv = $('#pnlGroupDataContent');
        if ($ScrollPosition.length && $ScrollingDiv.length) {
            // Store scrolling value
            $ScrollingDiv.scroll(function () {
                $ScrollPosition.val($ScrollingDiv.scrollTop());
            });
            // Set scrolling
            $ScrollingDiv.scrollTop($ScrollPosition.val());
        }
    
    0 讨论(0)
  • 2021-01-05 06:01

    Place something like:

     <asp:HiddenField id="hdnScrollPos" runat="server"/> in your aspx.
    

    Then, some javascript like:

    var hdnScroll = document.getElementById(<%=hdnScrollPos.ClientID%>);
    var bigDiv = document.getElementById('bigDiv');
    bigDiv.onscroll = function() {
         hdnScroll.value = bigDiv.scrollTop;
    }
    
    window.onload = function () { 
        bigDiv.scrollTop = hdnScroll.value;
    }
    
    0 讨论(0)
  • 2021-01-05 06:03

    ASP.NET has this built in all you need to do is include the MaintainScrollPositionOnPostback in your page directive.

    <%@ Page Language="C#" MaintainScrollPositionOnPostback="true" %>
    
    0 讨论(0)
提交回复
热议问题