阻止iframe中的网页重新加载回发

时间:2017-04-26 21:42:56

标签: javascript c# asp.net iframe

当父页面进行回发时,如何防止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>

1 个答案:

答案 0 :(得分:0)

您可以在UpdatePanel中包装将在回发期间更改的元素。然后在UpdatePanel之外提供iframe,在回发时不应重新加载。