Reload and remember the scroll position after coming back from popup

1.1k Views Asked by At

I have a page with products and each product has a link: "Buy". After clicking the link, a popup will show up which will have inside another .aspx file embedded. (the popup is generated by Colorbox plugin)

After the user is making the purchase (clicking on the button that sends to the DB information and so on): 1. the popup has to close itself 2. the parent page has to be reloaded 3. the scroll position has to be maintained

The problem is that the scroll position is not maintained (especially in IE browser).

What I tried: 1.

 MaintainScrollPositionOnPostback="true" -- had no effect

And:

Popup page:

<script>window.parent.callbackfromchild('" + ID + "');</script>

Parent page: function callbackfromchild(arg) {

            __doPostBack("callbackbtn", "");

            window.onload = function () {
                document.getElementById('#div' + arg).scrollIntoView(true);
            };

What am I doing wrong?

2

There are 2 best solutions below

0
On

You need this script in parent aspx page

<script>
        function callbackfromchild(id) {
         //maintain hash on post back
          var form = document.getElementById("<%=Page.Form.ClientID%>");
          // Change form action & post back
            form.action += '#'+ id;
          __doPostBack("<%=callbackbtn.ClientID%>", "");
        }
    </script>

And you need this anchor -with ID to be used a function parameter from popup- placed next to each product with different ids. So when URL of parent page reload with a hash added to it " page.aspx#x1" it will make the browser scrolls to that element.

<a id="x1"></a>

And I guess you already have this Linkbutton on parent page, will be target of postback

<asp:LinkButton ID="callbackbtn" runat="server" />

On colorbox popup you need to pass the anchor Id back to parent

window.parent.callbackfromchild('x1')

Or it that was a popup window , you would call it like this

window.opener.callbackfromchild('x1')
0
On

You can put current scroll position into cookie before opening popup and reapply it on window.onload, I use jQuery.scrollTo plugin for such tasks, it works perfect