Click here to Skip to main content
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

Introduction

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.

Code

 using System;
 using System.Web.UI;

 namespace AnnulMvtPostBack
 {
  
  public class GetPositionPostBack: 
    System.Web.UI.HtmlControls.HtmlInputHidden, 
    IPostBackDataHandler
  {
    //The position is saving in the viewstate.
    public int VPos
    {
      get 
      {
        if(ViewState["VPos"]==null)
        {
          ViewState["VPos"] = 0;
        }
        return (int)ViewState["VPos"];
      }
      set { ViewState["VPos"] = value; }
    }

    public int HPos
    {
      get
      {
        if(ViewState["HPos"]==null)
        {
          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(_Val.Length>1)
      {
        if(!HPos.ToString().Equals(_Val[0])  && _Val[0].Trim()!=null )
        {
          HPos= Int32.Parse(_Val[0]);
          _returnH=true;  
        }
        else _returnH=false;

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

      //_______return_________

      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>";
      if(!Page.IsClientScriptBlockRegistered("start"))
      {
        Page.RegisterClientScriptBlock("start",_start);
      }
      //_______________Move the scrollbar____________________________________
      string _goScroll = "<script language="'javascript'">scrollTo("
        + this.HPos + ","+this.VPos+");</script>";
      if(!Page.IsStartupScriptRegistered("goScroll"))
      {
        Page.RegisterStartupScript("goScroll",_goScroll);
      }
    }    
  }
}
      
<!--

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.

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

WebGourou

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

Comments and Discussions

 
QuestionRetain scroll position after postback Pinmemberskgup15-Nov-10 22:16 
Generalasp.net 2 provides easier way PinmemberMark Adamson25-Sep-06 4:11 
GeneralRe: asp.net 2 provides easier way PinmemberdB.22-Apr-07 8:31 
GeneralRe: asp.net 2 provides easier way Pinmembersnehasish25-Sep-08 22:04 
GeneralMore &quot;compliant&quot; code [modified] Pinmembernezza17-Aug-06 4:33 
GeneralQuestions for implementing code... PinmemberSomething Witty10-Mar-06 5:34 
GeneralRe: Questions for implementing code... PinmemberWebGourou11-Mar-06 9:25 
You have to use one of the 2 solutions. In the 2 situations it make the same.
 
On solution is:
A. <%@ Register Namespace="AnnulMvtPostBack" TagPrefix="g" Assembly="CHANGE_TO_ASSEMBLY_NAME" %>
in the <form> add
<g:AnnulMvtPostBack id="controls" runat="server"/>
 
or the method B. is also possible!
 
No need to add javascript the controls make it itself... Just declare it somewhere.
 
You have to choose Wink | ;)
 
Bye
 
WebGourou

 
WebGourou
 
-- modified at 15:26 Saturday 11th March, 2006
GeneralA small suggestion Pinmembermoredip8-Jan-05 6:41 
QuestionHow to use? PinmemberLittleVexy7-Mar-04 5:18 
AnswerHow to get it to Work... PinmemberLittleVexy7-Mar-04 5:51 
GeneralRe: How to get it to Work... PinmemberLittleVexy7-Mar-04 5:54 
GeneralEasier way is to set SmartNavigation=true PinmemberkamalM10-Sep-03 20:29 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberWebGourou11-Sep-03 4:55 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberZencat19-Aug-04 11:06 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberPlanetChaos12-Aug-04 6:23 
GeneralRe: Easier way is to set SmartNavigation=true PinmemberStaring at the Sun12-Jan-05 12:14 
GeneralOriginal PinmemberCypher3-Sep-03 3:28 
GeneralRe: Original Pinmemberbobjeey3-Sep-03 22:52 
GeneralRe: Original Pinmemberbobjeey4-Sep-03 1:04 
GeneralRe: Original PinmemberCypher4-Sep-03 2:27 
GeneralRe: Original PinmemberWebGourou4-Sep-03 9:20 
GeneralRe: Original Pinmembersam won7-Sep-03 23:30 
GeneralRe: Original PinmemberWebGourou8-Sep-03 7:05 
GeneralRe: Original PinsussBillBellivea9-Sep-03 13:19 
GeneralRe: Original Pinmembersam won9-Sep-03 15:27 
GeneralRe: Original PinmemberWebGourou10-Sep-03 0:39 
GeneralRe: Original PinmemberBillBelliveau10-Sep-03 8:21 
GeneralRe: Original Pinmembervicky_27-Nov-03 22:46 
GeneralRe: Original PinmemberWebGourou28-Nov-03 22:13 
GeneralRe: Original Pinmembervicky_30-Nov-03 21:43 
GeneralRe: Original PinsussAnonymous23-Jan-04 4:18 
GeneralRe: Original PinsussAnonymous23-Jan-04 4:19 
GeneralAnother cool script that works wonders : PinmemberCypher17-Feb-04 3:37 
GeneralRe: Original Pinmemberjoshi_vipul23-Aug-05 0:59 
GeneralRe: Original Pinmembersam won9-Sep-03 15:21 
GeneralRe: Original PinmemberWebGourou10-Sep-03 0:40 
GeneralRe: Original PinmemberDavid Bergeron19-Jan-04 8:49 
GeneralRe: So~ cool~!!!! Pinmemberz14318-Feb-05 4:42 
GeneralRe: Original PinsussAnonymous12-Sep-03 10:55 
GeneralRe: Original PinmemberWebGourou14-Sep-03 9:38 
GeneralRe: Original PinsussAnonymous15-Sep-03 22:10 
GeneralRe: Original PinmemberWebGourou16-Sep-03 3:00 

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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by WebGourou
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid