Click here to Skip to main content
Licence CPOL
First Posted 16 Jan 2006
Views 18,942
Bookmarked 20 times

Maintain and Control Scroll Position

By | 16 Jan 2006 | Article
Provides details of how to maintain scroll and how to control, i.e., reset the scroll when necessary.

Introduction

There are lots of solutions on the web to maintain scroll position during postback so I am not going to re-do any of those. Instead, you can go to http://aspnet.4guysfromrolla.com/articles/111704-1.aspx to see how to create a nice web control to maintain scroll position.

The additional part that I required was to maintain scroll position during the page, and then when a specific action occurred on the page, the scroll position would reset to the top.

I had a form that had two placeholders:

  1. For the user to input details, and
  2. To display the results to the user.

The first placeholder was quite long, so I needed to maintain the scroll position for the user, but when the user submits the form, I hide the first placeholder and display the results placeholder. If I just used the maintain feature, the results placeholder would not show from the top but from the scroll position of the submit button. Not very desirable.

Following is the code that you enter into your code-behind Page_Load method to make sure the scroll values are reset to zero for the results page.

private void Page_Load(object sender, System.EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("if (typeof(Page_ClientValidate) == 'function'){");
    sb.Append("if (Page_ClientValidate() == false){return false;}} ");
    sb.Append("document.forms['FormID'].scrollLeft.value = 0;" +
        "document.forms['FormID'].scrollRight.value = 0;");
    sb.Append(this.Page.GetPostBackEventReference(this.btnSubmit));
    sb.Append(";");
    this.btnSubmit.Attributes.Add("onclick", sb.ToString());
}

Hope this helps someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

kelebrowne

Web Developer

Australia Australia

Member



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
Generalwith asp.net 2.0 PinmemberMember #351951123:30 18 Feb '07  

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.120517.1 | Last Updated 16 Jan 2006
Article Copyright 2006 by kelebrowne
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid