Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com

Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
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.

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

Posted on Wednesday, February 17, 2016 9:36 PM ASP.NET | Back to top


Comments on this post: ScrollToControl helper method for ASP.Net web forms to move position to particular control

# re: ScrollToControl helper method for ASP.Net web forms to move position to particular control
Requesting Gravatar...
i like this tips, thx you Tips Kesehatan
Left by Muhamad Ilyas on Feb 24, 2016 3:06 PM

# re: ScrollToControl helper method for ASP.Net web forms to move position to particular control
Requesting Gravatar...
This is my first visit here and I am very happy to see informative information’s here. Well, my friends also worried so I will tell him about this site soon because they can learn from here something interesting.
phd essay writers
Left by Tracy Spears on May 29, 2018 4:22 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net