Click here to Skip to main content
6,634,665 members and growing! (15,095 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Reset the page scroll position after a PostBack

By WebGourou

Reset the page scroll position after a PostBack using a custom class derived from HtmlInputHidden
Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:31 Aug 2003
Views:168,532
Bookmarked:53 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.43 Rating: 3.43 out of 5
2 votes, 20.0%
1

2
2 votes, 20.0%
3
2 votes, 20.0%
4
4 votes, 40.0%
5

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);
      }
    }    
  }
}
      

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.

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


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

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
Generalasp.net 2 provides easier way PinmemberMark Adamson5:11 25 Sep '06  
GeneralRe: asp.net 2 provides easier way PinmemberdB.9:31 22 Apr '07  
GeneralRe: asp.net 2 provides easier way Pinmembersnehasish23:04 25 Sep '08  
GeneralMore "compliant" code [modified] Pinmembernezza5:33 17 Aug '06  
GeneralQuestions for implementing code... PinmemberSomething Witty6:34 10 Mar '06  
GeneralRe: Questions for implementing code... PinmemberWebGourou10:25 11 Mar '06  
GeneralA small suggestion Pinmembermoredip7:41 8 Jan '05  
GeneralHow to use? PinmemberLittleVexy6:18 7 Mar '04  
GeneralHow to get it to Work... PinmemberLittleVexy6:51 7 Mar '04  
GeneralRe: How to get it to Work... PinmemberLittleVexy6:54 7 Mar '04  
GeneralEasier way is to set SmartNavigation=true PinmemberkamalM21:29 10 Sep '03  
GeneralRe: Easier way is to set SmartNavigation=true PinmemberWebGourou5:55 11 Sep '03  
GeneralRe: Easier way is to set SmartNavigation=true PinmemberZencat12:06 19 Aug '04  
GeneralRe: Easier way is to set SmartNavigation=true PinmemberPlanetChaos7:23 12 Aug '04  
GeneralRe: Easier way is to set SmartNavigation=true PinmemberStaring at the Sun13:14 12 Jan '05  
GeneralOriginal PinmemberCypher4:28 3 Sep '03  
GeneralRe: Original Pinmemberbobjeey23:52 3 Sep '03  
GeneralRe: Original Pinmemberbobjeey2:04 4 Sep '03  
GeneralRe: Original PinmemberCypher3:27 4 Sep '03  
GeneralRe: Original PinmemberWebGourou10:20 4 Sep '03  
GeneralRe: Original Pinmembersam won0:30 8 Sep '03  
GeneralRe: Original PinmemberWebGourou8:05 8 Sep '03  
GeneralRe: Original PinsussBillBellivea14:19 9 Sep '03  
GeneralRe: Original Pinmembersam won16:27 9 Sep '03  
GeneralRe: Original PinmemberWebGourou1:39 10 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Aug 2003
Editor: Nishant Sivakumar
Copyright 2003 by WebGourou
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project