Click here to Skip to main content
15,881,600 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (29 votes)
21 Mar 2005 157.4K   51   18
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.

C#
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:

C#
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


Written By
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
nexsoftware17-Mar-14 7:44
nexsoftware17-Mar-14 7:44 
Question5 Stars Pin
User 785000623-Oct-13 5:10
User 785000623-Oct-13 5:10 
GeneralMy vote of 5 Pin
User 785000623-Oct-13 5:10
User 785000623-Oct-13 5:10 
GeneralMy vote of 5 Pin
lighthousekeeper5-Apr-13 11:05
lighthousekeeper5-Apr-13 11:05 
GeneralThanks!! Pin
xanderex17-Aug-10 13:26
xanderex17-Aug-10 13:26 
GeneralUsing in a Modal Popup Pin
Nate_115-Sep-09 10:32
Nate_115-Sep-09 10:32 
GeneralSlightly different scenario Pin
Mihai Drebot12-Apr-08 6:14
Mihai Drebot12-Apr-08 6:14 
GeneralHorizontal Scroll Pin
sree473-Aug-05 16:29
sree473-Aug-05 16:29 
GeneralI had trouble with the window.onload Pin
tanr16-Mar-05 16:53
tanr16-Mar-05 16:53 
GeneralRe: I had trouble with the window.onload Pin
Ach1lles21-Mar-05 0:07
Ach1lles21-Mar-05 0:07 
GeneralThis is only MSIE specific Pin
Stephan Pilz7-Mar-05 23:16
Stephan Pilz7-Mar-05 23:16 
Hello,

you should use document.getElementById instead of document.all, because document.all does not exist in the official javascript standard. It is only implemented in >= MSIE4.0. With these browsers, it works fine in javascript-blocks too, but exactly it's a JScript command.

document.getElementById is the official supported method by DOM. All Browser I know, supports this.

Best regards
Stephan
GeneralRe: This is only MSIE specific Pin
Ach1lles8-Mar-05 0:02
Ach1lles8-Mar-05 0:02 
GeneralActually - no Pin
lp3443-Mar-05 2:52
lp3443-Mar-05 2:52 
GeneralRe: Actually - no Pin
Ach1lles3-Mar-05 3:14
Ach1lles3-Mar-05 3:14 
GeneralRe: Actually - no Pin
lp3443-Mar-05 3:32
lp3443-Mar-05 3:32 
GeneralRe: Actually - no Pin
Ach1lles3-Mar-05 3:48
Ach1lles3-Mar-05 3:48 
GeneralRe: Actually - no Pin
Ashaman21-Mar-05 5:06
Ashaman21-Mar-05 5:06 
GeneralRe: Actually - no Pin
lp34421-Mar-05 20:53
lp34421-Mar-05 20:53 

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

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