2016-07-26 40 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

我找到了方法。回帖是模態退出的原因。解決方案是將其與更新面板一起封裝並解決問題。