Using this attribute worked great in Internet Explorer 6 and 8, Firefox, and Opera. It was only when I started testing in Chrome and Safari that I had problems. For some reason, neither of them would even recognize the fact that the window had been scrolled, so I had to come up with another solution for them.
1 <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" MaintainScrollPositionOnPostback="true" Inherits="_Default" %>
After reworking the solution to my problem, I was able to completely remove the 'MaintainScrollPositionOnPostBack' attribute and still have the page scroll to the same position it was in before the user clicked on the control. This turned out to be a good thing, because the 'MaintainScrollPositionOnPostBack' attribute interfered with functionality I added to the application later.
So the first thing I did was to remove the 'MaintainScrollPositionOnPostBack' property from the page directive.
Next, in the form tag, I added an 'onmousedown' eventhandler.
1 <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
16 <form id="form1" runat="server" onmousedown="findScrollPosition(event);">
The variables mouseX and mouseY represent the horizontal and vertical coordinates of the pointer relative to the window, when the mouse button was pushed.
As I said before, Chrome and Safari would not return the scroll properties properly, so I had to find another way to get the scroll value for those browsers. It just so happens, that I have had problems with those two browsers before, so I singled them out using their 'appVersion' property.
To calculate the scroll value for these two browsers, I found the x and y coordinates of the pointer relative to the whole document, by using the 'event.pageX' and 'event.pageY' properties.
To calculate the amount the document was scrolled, I subtracted the mouseX/Y values from the pageX/Y values.
For the other browsers, I just used the document's 'scrollLeft' and 'scrollTop' properties to retrieve the values.
44 function findScrollPosition(e)
46 var scrolledX;
47 var scrolledY;
48 var mouseX;
49 var mouseY;
51 mouseX = e.clientX;
52 mouseY = e.clientY;
56 var xWebkit = event.pageX;
57 var yWebkit = event.pageY;
58 scrolledX = xWebkit - mouseX;
59 scrolledY = yWebkit - mouseY;
63 scrolledX = document.documentElement.scrollLeft;
64 scrolledY = document.documentElement.scrollTop;
67 document.getElementById("scrolledX").value = scrolledX;
68 document.getElementById("scrolledY").value = scrolledY;
In the aspx file, I had placed two hidden controls to hold the scroll values so that they could be accessed later from the code behind.
82 <input type="hidden" id="scrolledX" runat="server" />
83 <input type="hidden" id="scrolledY" runat="server" />
5 Protected Sub PagePreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
6 ClientScript.RegisterStartupScript(Me.GetType(), "GetScrollBy", "getScrollBy();", True)
7 End Sub
73 function getScrollBy()
75 window.scrollBy(document.getElementById("scrolledX").value, document.getElementById("scrolledY").value);
And there you have it! Now, when if I have scrolled my window, and I click on a control, my page will not look like it has moved when it posts back.