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
            /// <summary>
            ///
            /// </summary>
            /// <param name=”page”></param>
            /// <param name=”clientId”></param>
            /// <param name=”alignToTop”></param>
            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
Advertisements