简体   繁体   English

从弹出窗口返回后重新加载并记住滚动位置

[英]Reload and remember the scroll position after coming back from popup

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. 单击链接后,将显示一个弹出窗口,该弹出窗口将嵌入另一个.aspx文件中。 (the popup is generated by Colorbox plugin) (弹出是由Colorbox插件生成的)

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 用户进行购买后(单击发送给数据库信息的按钮,依此类推):1.弹出窗口必须自行关闭2.父页面必须重新加载3.滚动位置必须保持

The problem is that the scroll position is not maintained (especially in IE browser). 问题是滚动位置未得到维护(尤其是在IE浏览器中)。

What I tried: 1. 我试过的:1。

 MaintainScrollPositionOnPostback="true" -- had no effect

And: 和:

Popup page: 弹出页面:

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

Parent page: function callbackfromchild(arg) { 父页面: function callbackfromchild(arg){

            __doPostBack("callbackbtn", "");

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

What am I doing wrong? 我究竟做错了什么?

You need this script in parent aspx page 您需要在父aspx页面中使用此脚本

<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. 而且,您需要在每个具有不同ID的产品旁边放置一个带有ID的锚点-带有ID,然后将其用作弹出窗口中的功能参数。 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. 因此,当父页面的URL重新加载并添加了哈希值“ page.aspx#x1”时,它将使浏览器滚动到该元素。

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

And I guess you already have this Linkbutton on parent page, will be target of postback 而且我想您已经在父页面上有了此Linkbutton,它将成为回发的目标

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

On colorbox popup you need to pass the anchor Id back to parent 在colorbox弹出窗口上,您需要将锚点ID传递回父级

window.parent.callbackfromchild('x1')

Or it that was a popup window , you would call it like this 或者这是一个弹出窗口,您可以这样称呼它

window.opener.callbackfromchild('x1')

您可以在打开弹出窗口之前将当前滚动位置放到cookie中,然后将其重新应用到window.onload上,我使用jQuery.scrollTo插件完成此类任务,效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM