2016-07-26 76 views
-1

我想在webforms中显示模式窗口。以下代码来自w3school,它工作正常,但在webforms中它的行为有所不同。该模式确实出现,但立即关闭。Modal显示,但立即在c#webforms中关闭

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="modal.WebForm4" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style> 
/* The Modal (background) */ 
.modal { 
    display: none; /* Hidden by default */ 
    position: fixed; /* Stay in place */ 
    z-index: 1; /* Sit on top */ 
    padding-top: 100px; /* Location of the box */ 
    left: 0; 
    top: 0; 
    width: 100%; /* Full width */ 
    height: 100%; /* Full height */ 
    overflow: auto; /* Enable scroll if needed */ 
    background-color: rgb(0,0,0); /* Fallback color */ 
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
} 

/* Modal Content */ 
.modal-content { 
    background-color: #fefefe; 
    margin: auto; 
    padding: 20px; 
    border: 1px solid #888; 
    width: 80%; 
} 

/* The Close Button */ 
.close { 
    color: #aaaaaa; 
    float: right; 
    font-size: 28px; 
    font-weight: bold; 
} 

.close:hover, 
.close:focus { 
    color: #000; 
    text-decoration: none; 
    cursor: pointer; 
} 
</style> 
</head> 
<body> 
    <form id="form1" runat="server"> 

    <div> 
    <h2>Modal Example</h2> 

    <asp:Button ID="myBtn" runat="server" Text="Button" type="button" /> 
<!-- Trigger/Open The Modal --> 
<button id="myBtn1" >Open Modal</button> 

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

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close">×</span> 
    <p>Some text in the Modal..</p> 
    </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 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> 
    &nbsp; 
    </div> 

    </form> 

     </body> 
</html> 
+0

它的因为它的一个webform它会尝试提交页面,基本上是用表单提交的数据重新加载页面,这就是为什么它看起来然后消失 –

回答

0

至于我,因为你使用的是<form>标签,当你按下一个按钮,它会尝试重新加载页面提交表单,也提到了。为了防止它提交它周围的一个办法是return falseonclick功能

// When the user clicks the button, open the modal 
btn.onclick = function() { 

    modal.style.display = "block"; 
    return false; 
} 

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

你不需要'span.onclick'功能 –

0

您不能将服务器端控件像这样,在JavaScript。改变你的代码。

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

    // Get the button that opens the modal 
    var btn = document.getElementById('<%=myBtn.ClientID%>'); 

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

    // When the user clicks the button, open the modal 
    btn.onclick = function() { 

     modal.style.display = "block"; 
     return false; 
    } 

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

我找到了方法。回帖是模态退出的原因。解决方案是将其与更新面板一起封装并解决问题。