Click here to Skip to main content
Licence 
First Posted 3 Mar 2005
Views 82,033
Bookmarked 49 times

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

By Ach1lles | 21 Mar 2005
This simple function uses JavaScript to scroll any control into view after the page has loaded.
3 votes, 12.0%
1

2
1 vote, 4.0%
3
3 votes, 12.0%
4
18 votes, 72.0%
5
4.68/5 - 25 votes
3 removed
μ 4.39, σa 2.39 [?]

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks!! Pinmemberxanderex14:26 17 Aug '10  
GeneralUsing in a Modal Popup PinmemberNate_111:32 15 Sep '09  
GeneralSlightly different scenario PinmemberMihai Drebot7:14 12 Apr '08  
GeneralHorizontal Scroll Pinmembersree4717:29 3 Aug '05  
GeneralI had trouble with the window.onload Pinmembertanr17:53 16 Mar '05  
GeneralRe: I had trouble with the window.onload PinmemberAch1lles1:07 21 Mar '05  
GeneralThis is only MSIE specific Pinmemberstp0:16 8 Mar '05  
GeneralRe: This is only MSIE specific PinmemberAch1lles1:02 8 Mar '05  
GeneralActually - no Pinmemberleopfeifenberger3:52 3 Mar '05  
GeneralRe: Actually - no PinmemberAch1lles4:14 3 Mar '05  
GeneralRe: Actually - no Pinmemberleopfeifenberger4:32 3 Mar '05  
GeneralRe: Actually - no PinmemberAch1lles4:48 3 Mar '05  
GeneralRe: Actually - no PinmemberAshaman6:06 21 Mar '05  
GeneralRe: Actually - no Pinmemberlp34421:53 21 Mar '05  

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.

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