Click here to Skip to main content
12,696,493 members (30,845 online)
Click here to Skip to main content
Add your own
alternative version

Stats

75.5K views
27 bookmarked
Posted

Persisting the scroll position of a DIV on AJAX postbacks

, 18 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how you can persist the scroll position of a DIV in AJAX postbacks.

Introduction

By default, the scroll position of DIV tags will be reset whenever you do a postback with AJAX. The following code can be used if you're working with AJAX and DIV-tags and you need to persist the scroll position of your DIV tag whenever an AJAX-postback occurs. I used the code in my own project, where I had to have an UpdatePanel and a GridView inside a fixed DIV-tag, while persisting the scroll position on Edit/Cancel/Update commands.

The code has been tested with IE6, IE7, FF 2.0.0.12, and Opera 9.26, and should basically work with any browser supporting the document.getElementById() method.

Background

I recently came across the prescribed problem above. So, I Googled and found an article on CodeProject (Persisting the scroll position of child DIV’s using MS AJAX) - however, the provided solution didn't work for me, so I decided to implement my own solution.

Using the code

Add the following code between your <head></head> or your <body></body> tags:

<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
    setScrollPos();
} 
function saveScrollPos(){
    document.getElementById("scrollPos").value = 
             document.getElementById("divScroll").scrollTop;
}
function setScrollPos(){
    document.getElementById("divScroll").scrollTop = 
             document.getElementById("scrollPos").value;
}
</script>

NB! You might have to change the scrollPos name with the ClientID if your element is encapsulated in another control, MasterPage, UpdatePanel etc. You can get the ClientID like this:

public static string scrollPos = String.Empty;

protected void Page_Load(object sender, EventArgs e){
  scrollPos = ((HtmlInputHidden)scrollPos).ClientID.ToString();
}

Then, change the JavaScript to:

<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
    setScrollPos();
} 
function saveScrollPos(){
    document.getElementById("<%=scrollPos%>").value = 
                document.getElementById("divScroll").scrollTop;
}
function setScrollPos(){
    document.getElementById("divScroll").scrollTop = 
                document.getElementById("<%=scrollPos%>").value;
}
</script>

Assuming that you have already added a <asp:ScriptManager> to your page, add the following:

<input type="hidden" id="scrollPos" name="scrollPos" value="0" runat="server"/>

<asp:UpdatePanel runat="server" ID="up1" UpdateMode="always">
<ContentTemplate>
<asp:Button runat="server" ID="button1" text="Post back!" />

<div id="divScroll" onscroll="saveScrollPos();" 
        style="height: 200px; overflow:auto; overflow-x:hidden; overflow-y:scroll;" >

</div>

</ContentTemplate> 
</asp:UpdatePanel>

Points of interest

The trick that made the code work compared to other resources I have looked at was the endRequest - see more here: PageRequestManagerEndRequestEvent.

Updates

  • 19-3-2008 - Fixed a couple of typos :-)

License

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

Share

About the Author

perels
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThanks a lot , Thank you very much Pin
S.Paul Mca24-Mar-16 1:37
memberS.Paul Mca24-Mar-16 1:37 
QuestionExcellent Pin
wadigzon Diaz-wong26-Aug-15 10:21
memberwadigzon Diaz-wong26-Aug-15 10:21 
GeneralSweet! Pin
Member 29253158-Feb-12 3:01
memberMember 29253158-Feb-12 3:01 
GeneralRe: Sweet! Pin
perels3-Jun-12 9:17
memberperels3-Jun-12 9:17 
GeneralThanks!! Pin
yonatankr16-Aug-09 21:21
memberyonatankr16-Aug-09 21:21 
GeneralRe: Thanks!! Pin
perels3-Jun-12 9:17
memberperels3-Jun-12 9:17 
QuestionHow to reset the scroll positon back to zero? Pin
bentonbenton12-May-09 13:19
memberbentonbenton12-May-09 13:19 
AnswerRe: How to reset the scroll positon back to zero? Pin
bentonbenton12-May-09 13:34
memberbentonbenton12-May-09 13:34 
GeneralRe: How to reset the scroll positon back to zero? Pin
perels21-Jul-09 13:03
memberperels21-Jul-09 13:03 
GeneralExcellent - Thank you Pin
thelizard2-May-09 9:52
memberthelizard2-May-09 9:52 
GeneralRe: Excellent - Thank you Pin
perels21-Jul-09 12:58
memberperels21-Jul-09 12:58 
GeneralThis is fantastic Pin
Sergodtud18-Mar-09 10:19
memberSergodtud18-Mar-09 10:19 
GeneralRe: This is fantastic Pin
perels21-Jul-09 12:55
memberperels21-Jul-09 12:55 
GeneralCouple tweaks Pin
maxismclaren9-Jan-09 12:02
membermaxismclaren9-Jan-09 12:02 
GeneralRe: Couple tweaks Pin
perels21-Jul-09 12:58
memberperels21-Jul-09 12:58 
GeneralThanks Pin
Saeedses30-Jun-08 1:01
memberSaeedses30-Jun-08 1:01 
GeneralRe: Thanks Pin
perels11-Jul-08 5:39
memberperels11-Jul-08 5:39 
GeneralGreat Pin
merlin98119-Mar-08 6:36
membermerlin98119-Mar-08 6:36 
GeneralRe: Great Pin
perels19-Mar-08 8:03
memberperels19-Mar-08 8:03 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 18 Mar 2008
Article Copyright 2008 by perels
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid