ScrollToControl helper method for ASP.Net web forms to move position to particular control

I've created a helper method for ASP.Net web forms to move position to particular control

            ///

            ///

            ///

            ///

            ///

            ///

            public static void ScrollToControl( Page page, string clientId, bool alignToTop)

            {

                //NOTE: if there are more than one call on the page, first one will take preference

                //If we want that last will take  preference, change key from MethodBase.GetCurrentMethod().Name to anchorName

                //recommended in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html               

                String script = " window.scrollTo = function () { };" + Environment.NewLine;

                script += String.Format("document.getElementById('{0}').scrollIntoView({1});" , clientId, alignToTop.JSToString());

                page.ClientScript.RegisterStartupScript(TypeForClientScript(), MethodBase.GetCurrentMethod().Name, script, true );

                //return script;

            }

            public static string JSToString(this bool bValue)

            {

                return bValue.ToString().ToLower();

            }

Use getElementById('{0}').scrollIntoView is simpler than location.hash , because you don't need to add extra anchor element.

Parameter alignToTop is very convenient to specify do you want to show control at the top or bottom of the screen.

It's posted as an answer http://stackoverflow.com/a/34823392/52277

There is more advance jQuery plug-in http://erraticdev.blogspot.com.au/2011/02/jquery-scroll-into-view-plugin-with.html

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.