0

I have a page with two drop down lists (ddlA and ddlB) Once the user selects an item from ddlA, it will populate items in ddlB I have auto post-back turned on for ddlA.

and since I wanted to maintain the scroll position, i turned on the MaintainScrollPositionOnPostBack to true like this in the page load method. :

this.MaintainScrollPositionOnPostBack = true;

But this doesn't seem to fix the issue.

Is there a workaround to fix the problem.?

-- UPDATE --

I added this js code to the page and now the problem is that the autopost back is never happening..

  <script type="text/javascript">
var xPos, yPos;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
prm.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
    xPos = $get('scrollDiv').scrollLeft;
    yPos = $get('scrollDiv').scrollTop;
}
function EndRequestHandler(sender, args) {
    $get('scrollDiv').scrollLeft = xPos;
    $get('scrollDiv').scrollTop = yPos;
}
    </script>

Am i adding the js code incorrectly? I found it here

psj01
  • 3,075
  • 6
  • 32
  • 63
  • http://stackoverflow.com/questions/7394852/how-to-maintain-page-scroll-position-after-a-page-postback-in-asp-net/7394874#7394874 – IrishChieftain Jun 29 '16 at 19:28
  • Is it the scroll position of the page body that you want to maintain? Or the scroll position inside a div element? I think that `MaintainScrollPositionOnPostBack` is for the page. – ConnorsFan Jun 29 '16 at 20:38

2 Answers2

0

Try to add this to the Page directive in your ASPX file and test it that route.

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" MaintainScrollPositionOnPostback="true" Inherits="_Default" %>
Kirk
  • 16,182
  • 20
  • 80
  • 112
0

I couldn't get MaintainScrollPositionOnPostback to work for me no matter what I tried. Based on this answer (https://stackoverflow.com/a/27505983) and a comment underneath it, I tried the following code which worked for me. This will only work if you have an ASP.NET ScriptManager (i.e. MicrosoftAjax.js) on your page. You also need JQuery added to your page. Add the below code to your .aspx file somewhere underneath asp:ScriptManager tag.

<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>

Basically we are holding the scroll position inside the value of a hidden field called hfPosition. Whenever the page is scrolled, the value will be updated. Then when a postback happens pageLoad() will automatically be called and will get the value of hfPosition and scroll to that value.

Including the ScriptManager and JQuery, my final code snippet looked something like this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script src="../Scripts/jquery-3.3.1.min.js" type="text/javascript"></script>
<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>/>
FTLturtle
  • 21
  • 2