title: How to avoid UpdatePanel scrolling on AutoPostBack?
body: <p>I have an ASP.NET FormView within an updatepanel. I'm auto-saving the form by setting AutoPostBack=true for each of the items within the FormView.</p>
  
  <p>This means the user can click a few elements in quick succession and fire off a few async postbacks almost simultaneously. </p>
  
  <p>The issue I have is that the user is able to keep scrolling down the form while the async postbacks are not yet complete. The browser always scrolls back to the position it was in at the first postback.</p>
  
  <p>Page.MaintainScrollPositionOnPostback is set to False.</p>
  
  <p>I've tried all sorts of things in ajax and jquery with:</p>
  
  <ul>
  <li>pageLoad</li>
  <li>add_initializeRequest</li>
  <li>add_endRequest</li>
  <li>document.ready</li>
  <li>etc.. </li>
  </ul>
  
  <p>but I always only seem to be able to access the scroll Y as it was on the first postback.</p>
  
  <p>Is there any way to retrieve the current scroll Y when the postback completes, so I can stop the scrolling occurring? Or perhaps is it possible to disable the scrolling behaviour?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to @chprpipr, I was able to get this to work. Here's my abbreviated solution:</p>
  
  <pre><code>var FormScrollerProto = function () {
      var Me = this;
      this.lastScrollPos = 0;
      var myLogger;
  
      this.Setup = function (logger) {
          myLogger = logger;
          // Bind a function to the window
          $(window).bind("scroll", function () {
              // Record the scroll position
              Me.lastScrollPos = Me.GetScrollTop();
              myLogger.Log("last: " + Me.lastScrollPos);
          });
      }
  
      this.ScrollForm = function () {
          // Apply the last scroll position
          $(window).scrollTop(Me.lastScrollPos);
      }
  
      // Call this in pageRequestManager.EndRequest
      this.EndRequestHandler = function (args) {
          myLogger.Log(args.get_error());
          if (args.get_error() == undefined) {
              Me.ScrollForm();
          }
      }
  
      this.GetScrollTop = function () {
          return Me.FilterResults(
                  window.pageYOffset ? window.pageYOffset : 0,
                  document.documentElement ? document.documentElement.scrollTop : 0,
                  document.body ? document.body.scrollTop : 0
              );
      }
  
      this.FilterResults = function (n_win, n_docel, n_body) {
          var n_result = n_win ? n_win : 0;
          if (n_docel &amp;&amp; (!n_result || (n_result &gt; n_docel)))
              n_result = n_docel;
          return n_body &amp;&amp; (!n_result || (n_result &gt; n_body)) ? n_body : n_result;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Main page:</p>
  
  <pre><code>...snip...
  
  var logger;
      var FormScroller;
  
      // Hook up Application event handlers.
      var app = Sys.Application;
  
      // app.add_load(ApplicationLoad); - use pageLoad instead
      app.add_init(ApplicationInit);
      // app.add_disposing(ApplicationDisposing);
      // app.add_unload(ApplicationUnload);
  
      // Application event handlers for component developers.
      function ApplicationInit(sender) {
          var prm = Sys.WebForms.PageRequestManager.getInstance();
          if (!prm.get_isInAsyncPostBack()) {
              prm.add_initializeRequest(InitializeRequest);
              prm.add_beginRequest(BeginRequest);
              prm.add_pageLoading(PageLoading);
              prm.add_pageLoaded(PageLoaded);
              prm.add_endRequest(EndRequest);
          }
  
          // Set up components
          logger = new LoggerProto();
          logger.Init(true);
          logger.Log("APP:: Application init.");
  
          FormScroller = new FormScrollerProto();
      }
  
      function InitializeRequest(sender, args) {
          logger.Log("PRM:: Initializing async request.");
          FormScroller.Setup(logger);
      }
  
  ...snip...
  
  function EndRequest(sender, args) {
          logger.Log("PRM:: End of async request.");
  
          maintainScroll(sender, args);
  
          // Display any errors
          processErrors(args);
      }
  
  ...snip...
  
  function maintainScroll(sender, args) {
          logger.Log("maintain: " + winScrollTop);
          FormScroller.EndRequestHandler(args);
      }
  </code></pre>
  
  <p>I also tried calling the EndRequestHandler (had to remove the args.error check) to see if it reduced flicker when scrolling but it doesn't. It's worth noting that the perfect solution would be to stop the browser trying to scroll at all - right now there is a momentary jitter which would not be acceptable in apps with a large user base.</p>
  
  <p>(The scroll top code is not mine - found it on the web.)</p>
  
  <p>(Here's a helpful MSDN page for the clientside lifecycle: <a href="http://msdn.microsoft.com/en-us/library/bb386417.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/bb386417.aspx</a>)</p>
  
  <hr>
  
  <p>Update 7 March:</p>
  
  <p>I just found an extremely simple way to do this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  
  prm.add_beginRequest(beginRequest);
  
  function beginRequest()
  {
      prm._scrollPosition = null;
  }
  
  &lt;/script&gt;
  </code></pre>