Click here to Skip to main content
Click here to Skip to main content

How to scroll an ASP.NET control into view after page load using codebehind

By , 21 Mar 2005
 

Introduction

In some cases when an ASP.NET page loads the control, you need to focus on is not visible because it is further down the page. I have had numerous occasions when a request variable indicates which item on a long list the user is interested in viewing, this script can help by scrolling the particular item into view.

Code

The following function I have added to a Utils.dll library for general use so is static and needs the current page as a variable.

public class Utils
{
   public static void FocusControlOnPageLoad(string ClientID, 
                                       System.Web.UI.Page page)

   {

      page.RegisterClientScriptBlock("CtrlFocus",

      @"<script> 

      function ScrollView()

      {
         var el = document.getElementById('"+ClientID+@"')
         if (el != null)
         {        
            el.scrollIntoView();
            el.focus();
         }
      }

      window.onload = ScrollView;

      </script>");

   }
}

You can use this as follows:

private void Page_Load(object sender, System.EventArgs e)
{
Utils.FocusControlOnPageLoad(this.yourcontrol.ClientID, this.Page);
}

Hopes this helps someone.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ach1lles
Founder Tech Dept
United Kingdom United Kingdom
Member
My Evolution:
TRS-80 Basic, Clipper, C, Better Basic, FORTRAN, C++, Visual Basic, Delphi, C#

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberlighthousekeeper5 Apr '13 - 11:05 
GeneralThanks!!memberxanderex17 Aug '10 - 13:26 
GeneralUsing in a Modal PopupmemberNate_115 Sep '09 - 10:32 
GeneralSlightly different scenariomemberMihai Drebot12 Apr '08 - 6:14 
GeneralHorizontal Scrollmembersree473 Aug '05 - 16:29 
GeneralI had trouble with the window.onloadmembertanr16 Mar '05 - 16:53 
GeneralRe: I had trouble with the window.onloadmemberAch1lles21 Mar '05 - 0:07 
GeneralThis is only MSIE specificmemberstp7 Mar '05 - 23:16 
GeneralRe: This is only MSIE specificmemberAch1lles8 Mar '05 - 0:02 
GeneralActually - nomemberleopfeifenberger3 Mar '05 - 2:52 
GeneralRe: Actually - nomemberAch1lles3 Mar '05 - 3:14 
GeneralRe: Actually - nomemberleopfeifenberger3 Mar '05 - 3:32 
GeneralRe: Actually - nomemberAch1lles3 Mar '05 - 3:48 
GeneralRe: Actually - nomemberAshaman21 Mar '05 - 5:06 
GeneralRe: Actually - nomemberlp34421 Mar '05 - 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 21 Mar 2005
Article Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid