Introduction
Added space to demonstrate the issue.
When you click the "Submit" button below. It should not scroll back to the top.
This I Didn't Tried
Didn't bother trying the various methods of setting the Page Property "MantainScrollPositionOnPostback" because several posts said it doesn't work.
This I Tried
Method 1:
Worked locally. Didn't work on Host.
<script>
(function () {
var originalValidationSummaryOnSubmit = window.ValidationSummaryOnSubmit;
window.ValidationSummaryOnSubmit = function (validationGroup) {
var originalScrollTo = window.scrollTo;
window.scrollTo = function () { };
originalValidationSummaryOnSubmit(validationGroup);
window.scrollTo = originalScrollTo;
}
}());
</script>
Method 2:
Worked locally. Didn't work on Host.
<script type="text/javascript">
window.scrollTo = function( x,y ) {
return true;
}
</script>
Method 3:
Didn't work locally.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanelContent" runat="server" UpdateMode="Conditional">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField runat="server" ID="hfPosition" Value="" />
<script type="text/javascript">
$(function () {
var positionField = $("#<%=hfPosition.ClientID%>");
window.onscroll = function () {
var position = $(window).scrollTop();
positionField.val(position);
};
});
function pageLoad() {
var positionField = $("#<%=hfPosition.ClientID%>");
var position = parseInt(positionField.val());
if (!isNaN(position)) {
$(window).scrollTop(position);
}
};
</script>/>
Method 4:
Worked locally, but disabled Validation Summary. Did not work on Host.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Inherits="_default" MaintainScrollPositionOnPostback="true" %>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="kb-text-error" Visible="True" DisplayMode="List" EnableClientScript="false" />
References
https://stackoverflow.com/questions/2613411/how-can-i-prevent-a-page-to-jump-to-top-position-after-failed-validation
https://stackoverflow.com/questions/699171/asp-net-validation-summary-causes-page-to-jump-to-top/4962195