cgval cgval - 2 months ago 12
ASP.NET (C#) Question

Maintain Scroll Bar position of a div within a gridview after a PostBack

I used the following piece of code in the web.config in order to maintain the scrollbar position after a server postback:

<pages maintainScrollPositionOnPostBack="true" >

All is working fine, but now i have a gridview encapsuled within a div with a scrollbar in the div (internal scrollbar).

When an event occur on one of the rows inside the gridview, the internal scrollbar doesn't maintain its original position unlike the outer one.

Any ideas?


For future reference:

The normal procedure is to write the following in the web.config file:

    <pages maintainScrollPositionOnPostBack="true" >

This will preserve the scroll bar position of all web pages.

If you have a scroll bar within a gridview (or div) then use the following script:

<script type="text/javascript">
    window.onload = function () {
        var strCook = document.cookie;
        if (strCook.indexOf("!~") != 0) {
            var intS = strCook.indexOf("!~");
            var intE = strCook.indexOf("~!");
            var strPos = strCook.substring(intS + 2, intE);
            document.getElementById("grdWithScroll").scrollTop = strPos;
    function SetDivPosition() {
        var intY = document.getElementById("grdWithScroll").scrollTop;
        document.cookie = "yPos=!~" + intY + "~!";

And the div must be as follows:

<div id="grdWithScroll" …………  onscroll="SetDivPosition()">