Click here to Skip to main content
11,414,234 members (73,223 online)
Click here to Skip to main content

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

, 21 Mar 2005
Rate this:
Please Sign up or sign in to vote.
This simple function uses JavaScript to scroll any control into view after the page has loaded.

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

Share

About the Author

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

Comments and Discussions

 
QuestionThanks! Pin
nexsoftware, 17-Mar-14 8:44
membernexsoftware17-Mar-14 8:44 
Question5 Stars Pin
Tuyen Nguyen GB, 23-Oct-13 6:10
memberTuyen Nguyen GB23-Oct-13 6:10 
GeneralMy vote of 5 Pin
Tuyen Nguyen GB, 23-Oct-13 6:10
memberTuyen Nguyen GB23-Oct-13 6:10 
GeneralMy vote of 5 Pin
lighthousekeeper, 5-Apr-13 12:05
memberlighthousekeeper5-Apr-13 12:05 
GeneralThanks!! Pin
xanderex, 17-Aug-10 14:26
memberxanderex17-Aug-10 14:26 
GeneralUsing in a Modal Popup Pin
Nate_1, 15-Sep-09 11:32
memberNate_115-Sep-09 11:32 
GeneralSlightly different scenario Pin
Mihai Drebot, 12-Apr-08 7:14
memberMihai Drebot12-Apr-08 7:14 
GeneralHorizontal Scroll Pin
sree47, 3-Aug-05 17:29
membersree473-Aug-05 17:29 
GeneralI had trouble with the window.onload Pin
tanr, 16-Mar-05 17:53
membertanr16-Mar-05 17:53 
GeneralRe: I had trouble with the window.onload Pin
Ach1lles, 21-Mar-05 1:07
memberAch1lles21-Mar-05 1:07 
Thanks for spotting that. Have changed the code.
GeneralThis is only MSIE specific Pin
stp, 8-Mar-05 0:16
memberstp8-Mar-05 0:16 
GeneralRe: This is only MSIE specific Pin
Ach1lles, 8-Mar-05 1:02
memberAch1lles8-Mar-05 1:02 
GeneralActually - no Pin
leopfeifenberger, 3-Mar-05 3:52
memberleopfeifenberger3-Mar-05 3:52 
GeneralRe: Actually - no Pin
Ach1lles, 3-Mar-05 4:14
memberAch1lles3-Mar-05 4:14 
GeneralRe: Actually - no Pin
leopfeifenberger, 3-Mar-05 4:32
memberleopfeifenberger3-Mar-05 4:32 
GeneralRe: Actually - no Pin
Ach1lles, 3-Mar-05 4:48
memberAch1lles3-Mar-05 4:48 
GeneralRe: Actually - no Pin
Ashaman, 21-Mar-05 6:06
memberAshaman21-Mar-05 6:06 
GeneralRe: Actually - no Pin
lp344, 21-Mar-05 21:53
memberlp34421-Mar-05 21:53 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.2 | Last Updated 21 Mar 2005
Article Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid