【问题标题】:Stop webpage in iframe from reloading on postback阻止 iframe 中的网页在回发时重新加载
【发布时间】:2017-09-24 11:52:39
【问题描述】:

当父页面进行回发时,如何防止 iframe 中的网页重新加载? 下面是一个简单的 .aspx 页面。当我单击执行回发的 Button1 时,将重新加载以 blank.aspx 作为其源的 iframe。我怎样才能防止这种情况发生?代码如下。

       <div>
        This is an empty page with a button to open a modal window in an iframe
        <asp:Button ID="Button1" runat="server" Text="Postback" OnClick="Button1_Click" />
    &nbsp;<asp:Label ID="lblPostback" runat="server" Text="Initial Load"></asp:Label>
                <!-- Trigger/Open The Modal -->
<button id="myBtn" type="button">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
      <iframe src="Site/Public/blank.aspx"></iframe>
  </div>

</div>
        <script>
        // Get the modal
        var modal = document.getElementById('myModal');

        // Get the button that opens the modal
        var btn = document.getElementById("myBtn");

        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];

        // When the user clicks on the button, open the modal
        btn.onclick = function () {
            modal.style.display = "block";
        }

        // When the user clicks on <span> (x), close the modal
        span.onclick = function () {
            modal.style.display = "none";
        }

         //When the user clicks anywhere outside of the modal, close it
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    </script>
    </div>

【问题讨论】:

    标签: javascript c# asp.net iframe


    【解决方案1】:

    您可以将回发期间将更改的元素包装在 UpdatePanel 中。如果 iframe 位于 UpdatePanel 之外,则在回发时不应重新加载。

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 2013-11-19
      • 2014-02-05
      • 2011-01-13
      • 1970-01-01
      • 2023-03-31
      • 2018-08-19
      • 2014-09-23
      • 1970-01-01
      相关资源
      最近更新 更多