Keeping the scroll position of a div after post back

Hi

A few days back I came up with an interesting requirement. I was working on a page, which had a scrollable div. The problem was that after the post back the div would loose its scroll position. Now when there is a lot of data it is very inconvenient for the user to again scroll back to the position where he was before the post back.

One of my colleagues helped me out with the solution. The solution was quite simple. We saved the value of the scroll in a hidden variable. This was done on the onscroll event of the div. On the page load event of the page we again set the scroll of the div to the value saved in the hidden variable. That’s all. Here is a simple code to show the actual working.

<body onload="SetVikScroll()">
    <form id="form1" runat="server">
        <div id="VikramDiv" onscroll="saveScrollPos();" style="height:50px; width:100%; overflow:auto;">

<!--Some stuff to get some scroll in the div -->

         </div>

        <asp:Button runat="server" ID="buttonForPostBack" Text="Post Back Button" />

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

    </form>

    <script type="text/javascript" language="javascript">
        function saveScrollPos()
        {

            document.getElementById(“VikramscrollPos”).value = document.getElementById(“VikramDiv”).scrollTop;

        }

        function SetVikScroll()
        {

            document.getElementById(“VikramDiv”).scrollTop = document.getElementById(“VikramscrollPos”).value;

        }
    </script> 
</body>

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 6/12/2007 8:47:22 AM

Does it work in all browsers?

Posted on 6/12/2007 9:51:49 AM

Hi Josh
I was working on a IE only project, so did not checked in other browser. If you have problem in other browser, tell me

Posted on 7/15/2007 6:57:44 AM

vikram wont this page directives "MaintainScrollPositionOnPostback="true"" will help to handle scroll position..?
Cheers,
Vikram

Posted on 7/15/2007 8:56:12 PM

Hi Josh,

Yes you are right. This directive will work. But it is only available in asp.net 2.0. Also the directive has some problem when used in a user control

Posted on 7/15/2007 8:56:42 PM

Hi Josh,

Yes you are right. This directive will work. But it is only available in asp.net 2.0. Also the directive has some problem when used in a user control

Posted on 12/15/2007 4:58:41 PM

I have the same issue, but the scrollable division is in the master page.
I tried your approche but the only flaw in it is that the hidden input does not save it's content after postback, theefore it is not working.

If you haveincountered same problem and have a fix please post it.
Thanks

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia