title: How can I scroll an ASP.NET page under control of the code-behind page?
body: <p>I have an ASP.NET 3.5 page with some AJAX, some Validators and some business-rule validation done in the code-behind page (VB.NET).</p>
  
  <p>The Validators set focus to the 'offending' control when an error is detected.  </p>
  
  <p>When a business rule is violated, I used the following code to generate some javascript "on the fly" and execute it to simulate the Winforms "MessageBox.Show" functionality:</p>
  
  <pre><code>Sub DisplayMessages()
      Dim lblError As New Label
      lblError.Text = "&lt;script language='javascript'&gt;" &amp; Environment.NewLine &amp; "window.alert('"
  + strError + "')&lt;/script&gt;"
      Page.Controls.Add(lblError)
      strError = "" 
  End Sub
  </code></pre>
  
  <p>(and the appropriate calling of that subroutine when necessary)</p>
  
  <p>Now, when the user hits the "Save" button and everything is ok, the page stays scrolled down at the bottom where the Save button is.  I want to give the user an indication that things finished successfully by crolling the page back to the top.</p>
  
  <p>I tried to adapt the previous use of javascript to this with another subroutine - adding the control didn't seem to work so I tried to just put the raw code in to execute ad hoc:</p>
  
  <pre><code>Sub ScrollPageToTop()
      Dim strScroll As String
      strScroll = "&lt;script language='javascript'&gt;" &amp; Environment.NewLine &amp; "window.scrollTo(0,0);&lt;/script&gt;"
      ClientScript.RegisterStartupScript(Me.GetType, "ScrollToTop", strScroll.ToString)
  End Sub
  </code></pre>
  
  <p>At the end of the "btnSave.Click" code, if everything went ok, calling that subroutine.</p>
  
  <p>What I get is the page scrolling to the top and then back to where it was when the Save button was clicked.</p>
  
  <p>For various reasons, 'MaintainScrollPositionOnPostback="True"' is in the Page directives at the top of the .aspx code.  What I'd really like is to make that FALSE for just a moment so that the page resets at the top - again, only if everything is ok and only for this button (there are other buttons on the page that require the page to keep it's scroll position).</p>