Raghavendra U

Raghavendra U

  • 717
  • 1.3k
  • 444.9k

Keep the scroll position of a DIV after postback

May 5 2009 8:31 AM
<body onload="setScrollPos('myDiv')">

 

  <form id="form1" runat="server">
 
  <div id="myDiv" onscroll="saveScrollPos(this);" style="height:50px; width:100%; overflow:auto;">
  ...
  </div>

 

  <asp:Button runat="server" ID="Button1" Text="Press Me!" />

 

  <input type="hidden" id="scrollPos" name="scrollPos" value="0" runat="server"/>
 
  </form>
 
  <script type="text/javascript" language="javascript">
 
  function saveScrollPos(object)
  {
  document.getElementById('scrollPos').value = object.scrollTop;
  }

 

  function setScrollPos(elementId)
  {
  document.getElementById(elementId).scrollTop = document.getElementById('scrollPos').value;
  }
 
  </script>

 

</body>

Answers (3)