Click here to Skip to main content
13,353,462 members (39,139 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 31 Aug 2003

Reset the page scroll position after a PostBack

, 31 Aug 2003
Rate this:
Please Sign up or sign in to vote.
Reset the page scroll position after a PostBack using a custom class derived from HtmlInputHidden


This code cancel the move of the scrollbar after a Postback. It is a simple Control based on HtmlInputHidden which saves, before the postback, the position of the scrollbar and moves it afterwards back to the old position. The variable is saved in the viewstate. This code uses JavaScript to get the position of the scrollbar and move it.


 using System;
 using System.Web.UI;

 namespace AnnulMvtPostBack
  public class GetPositionPostBack: 
    //The position is saving in the viewstate.
    public int VPos
          ViewState["VPos"] = 0;
        return (int)ViewState["VPos"];
      set { ViewState["VPos"] = value; }

    public int HPos
          ViewState["HPos"] = 0;
        return (int)ViewState["HPos"];
      set { ViewState["HPos"] = value; }
    //Here the value of the HiddenControl is catching 
    //before the PostBack
    public bool LoadPostData(String postDataKey, 
      System.Collections.Specialized.NameValueCollection values) 
      bool _returnV;
      bool _returnH;
      string Val = values[this.UniqueID].Trim();
      char Eperluette = Char.Parse("&");
      string[] _Val = Val.Split(Eperluette);
        if(!HPos.ToString().Equals(_Val[0])  && _Val[0].Trim()!=null )
          HPos= Int32.Parse(_Val[0]);
        else _returnH=false;

        if(!VPos.ToString().Equals(_Val[1])  && _Val[1].Trim()!=null )
          VPos= Int32.Parse(_Val[1]);
        else _returnV=false;  
        HPos = 0;
        return false; //return true to execute RaisePostDataChangedEvent();


      if(_returnV || _returnH) return false; 
       //return true to execute RaisePostDataChangedEvent();
      else return false;    
    //This function is here just to understand the LoadPostData 
    //with the RaisePostDataChangedEvent() 
    public void RaisePostDataChangedEvent() 
      // Part of the IPostBackDataHandler contract.  Invoked 
      // if we ever returned true from the
      // LoadPostData method (indicates that we want a change 
      // notification raised).  Since we
      // always return false, this method is just a no-op.

    //The control is rendered with a UniqueId and Javascript Code 
    //is use to get the position of the scrollbar
    protected override void OnPreRender(EventArgs e) 
      string _start = "<script language="'Javascript'">onscroll = function(){";
      _start += "document.getElementById('" + this.UniqueID + 
        "').value = document.body.scrollLeft+'&'+document.body.scrollTop;";
      _start += "}</script>";
      //_______________Move the scrollbar____________________________________
      string _goScroll = "<script language="'javascript'">scrollTo("
        + this.HPos + ","+this.VPos+");</script>";

Background (optional)

Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Using the code


I just want to say that I have stolen this idea from another one on the Web (I don't remember who :( ) but I wasn't able to use it so I made my own code.

Mini Tip

  • Don't forget to load it like a control.


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

France France
Young developer C#, Javascript, and database but just for fun...

You may also be interested in...

Comments and Discussions

QuestionRetain scroll position after postback Pin
skgup15-Nov-10 23:16
memberskgup15-Nov-10 23:16 2 provides easier way Pin
Mark Adamson25-Sep-06 5:11
memberMark Adamson25-Sep-06 5:11 
This has now been made possible in ASP.NET 2 by putting

maintainScrollPositionOnPostBack=true in the page tag.

It is cross browser too.


GeneralRe: 2 provides easier way Pin
dB.22-Apr-07 9:31
memberdB.22-Apr-07 9:31 
GeneralRe: 2 provides easier way Pin
snehasish25-Sep-08 23:04
membersnehasish25-Sep-08 23:04 
GeneralMore &quot;compliant&quot; code [modified] Pin
nezza17-Aug-06 5:33
membernezza17-Aug-06 5:33 
GeneralQuestions for implementing code... Pin
Something Witty10-Mar-06 6:34
memberSomething Witty10-Mar-06 6:34 
GeneralRe: Questions for implementing code... Pin
WebGourou11-Mar-06 10:25
memberWebGourou11-Mar-06 10:25 
GeneralA small suggestion Pin
moredip8-Jan-05 7:41
membermoredip8-Jan-05 7:41 
QuestionHow to use? Pin
LittleVexy7-Mar-04 6:18
memberLittleVexy7-Mar-04 6:18 
AnswerHow to get it to Work... Pin
LittleVexy7-Mar-04 6:51
memberLittleVexy7-Mar-04 6:51 
GeneralRe: How to get it to Work... Pin
LittleVexy7-Mar-04 6:54
memberLittleVexy7-Mar-04 6:54 
GeneralEasier way is to set SmartNavigation=true Pin
kamalM10-Sep-03 21:29
memberkamalM10-Sep-03 21:29 
GeneralRe: Easier way is to set SmartNavigation=true Pin
WebGourou11-Sep-03 5:55
memberWebGourou11-Sep-03 5:55 
GeneralRe: Easier way is to set SmartNavigation=true Pin
Zencat19-Aug-04 12:06
memberZencat19-Aug-04 12:06 
GeneralRe: Easier way is to set SmartNavigation=true Pin
PlanetChaos12-Aug-04 7:23
memberPlanetChaos12-Aug-04 7:23 
GeneralRe: Easier way is to set SmartNavigation=true Pin
Staring at the Sun12-Jan-05 13:14
memberStaring at the Sun12-Jan-05 13:14 
GeneralOriginal Pin
Cypher3-Sep-03 4:28
memberCypher3-Sep-03 4:28 
GeneralRe: Original Pin
bobjeey3-Sep-03 23:52
memberbobjeey3-Sep-03 23:52 
GeneralRe: Original Pin
bobjeey4-Sep-03 2:04
memberbobjeey4-Sep-03 2:04 
GeneralRe: Original Pin
Cypher4-Sep-03 3:27
memberCypher4-Sep-03 3:27 
GeneralRe: Original Pin
WebGourou4-Sep-03 10:20
memberWebGourou4-Sep-03 10:20 
GeneralRe: Original Pin
sam won8-Sep-03 0:30
membersam won8-Sep-03 0:30 
GeneralRe: Original Pin
WebGourou8-Sep-03 8:05
memberWebGourou8-Sep-03 8:05 
GeneralRe: Original Pin
BillBellivea9-Sep-03 14:19
sussBillBellivea9-Sep-03 14:19 
GeneralRe: Original Pin
sam won9-Sep-03 16:27
membersam won9-Sep-03 16:27 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by WebGourou
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid