Javascript Scroll to Top of Page for Post Back Element ID

Posted: August 24, 2010 in ASP.Net, Javascript

You can use the following piece of Javascript to scroll to the top of a page based on a post back element id:

Javascript
Copy this piece of code into your ASP.Net page.

 
<script type="text/javascript">
   //-----------------------------------------------------
   // Script to get the element that caused the post back
   //-----------------------------------------------------
    var postBackElementID;
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(InitializeRequestHandler);
    function InitializeRequestHandler(sender, args){postBackElementID = args.get_postBackElement().id.substring(args.get_postBackElement().id.lastIndexOf("_") + 1);}

   Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

   //------------------------------------------------------------------
   // Function to handle scrolling to top of page between wizard steps
   //------------------------------------------------------------------
   function EndRequestHandler(sender, args)
   {
      if (postBackElementID == "YourElementID1" | postBackElementID == "YourElementID2") 
      {   
          scrollTo(0,0);
      }
   }
</script> 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s