Click here to Skip to main content
11,410,750 members (62,752 online)
Click here to Skip to main content

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 Frown | :( ) 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...

Comments and Discussions

QuestionRetain scroll position after postback Pinmemberskgup15-Nov-10 23:16 2 provides easier way PinmemberMark Adamson25-Sep-06 5:11 
GeneralRe: 2 provides easier way PinmemberdB.22-Apr-07 9:31 
GeneralRe: 2 provides easier way Pinmembersnehasish25-Sep-08 23:04 
GeneralMore &quot;compliant&quot; code [modified] Pinmembernezza17-Aug-06 5:33 
GeneralQuestions for implementing code... PinmemberSomething Witty10-Mar-06 6:34 
GeneralRe: Questions for implementing code... PinmemberWebGourou11-Mar-06 10:25 
GeneralA small suggestion Pinmembermoredip8-Jan-05 7:41 
QuestionHow to use? PinmemberLittleVexy7-Mar-04 6:18 
AnswerHow to get it to Work... PinmemberLittleVexy7-Mar-04 6:51 
Ok, I figured it out. For most of you who are having problems:

1) Put source in your project, as it is.

2) Open your ASPX page, in HTML view.

3) Add this, after the first line:

<%@ Register Namespace="AnnulMvtPostBack" TagPrefix="g" Assembly="_NAME_OF_YOUR_PROJECT_GOES_HERE_" %>

4) Scroll down, and after <form ....> tag put this:

<g:GetPositionPostBack runat="server">

Thats it.. Enjoy Smile | :)

GeneralRe: How to get it to Work... PinmemberLittleVexy7-Mar-04 6:54 
GeneralEasier way is to set SmartNavigation=true PinmemberkamalM10-Sep-03 21:29 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberWebGourou11-Sep-03 5:55 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberZencat19-Aug-04 12:06 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberPlanetChaos12-Aug-04 7:23 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberStaring at the Sun12-Jan-05 13:14 
GeneralOriginal PinmemberCypher3-Sep-03 4:28 
GeneralRe: Original Pinmemberbobjeey3-Sep-03 23:52 
GeneralRe: Original Pinmemberbobjeey4-Sep-03 2:04 
GeneralRe: Original PinmemberCypher4-Sep-03 3:27 
GeneralRe: Original PinmemberWebGourou4-Sep-03 10:20 
GeneralRe: Original Pinmembersam won8-Sep-03 0:30 
GeneralRe: Original PinmemberWebGourou8-Sep-03 8:05 
GeneralRe: Original PinsussBillBellivea9-Sep-03 14:19 
GeneralRe: Original Pinmembersam won9-Sep-03 16:27 

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
Web01 | 2.8.150414.5 | Last Updated 1 Sep 2003
Article Copyright 2003 by WebGourou
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid