[英]Show alert error message when certain condition arises
我在頁面加載時有以下代碼,用於顯示錯誤警報:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["message"] == "noemployees")
AlertDanger.Visible = true;
這是調用錯誤的地方。 頁面重新加載並顯示錯誤。
if (payFitments == null)
{
Response.Redirect("Default?message=noemployees");
}
我有以下標記
<script type="text/javascript">
$(document).ready(function () {
window.setTimeout(function () {
$(".alert").fadeTo(1500, 0).slideUp(500, function () {
$(this).remove();
});
}, 3000);
});
-------------------------
<div runat="server" visible="false" id="AlertDanger" class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>You must have at least one employee to process</strong>
</div>
如何在無需再次加載默認頁面的情況下顯示此消息? 在 web 上沒有看到清楚地表明這一點的示例。
您正在使用JQuery
remove
,它有效地從您的 DOM 中刪除警報。 因此,在不重新加載頁面的情況下再次顯示它是不可能的。
但是您可以改用JQuery
detach
。 然后您可以使用appendTo
再次插入它。
var alert = null; function showHideAlert() { if (alert) { alert.appendTo("body"); alert = null; } else { alert = $(".alert").detach(); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <body> <button onclick="showHideAlert();">Show/Hide alert</button> <div class="alert">ALERT!!!</div> </body>
嘗試提供 static html 文件,然后在 Z9E13B69D1D22DA927Z102ACAAAF 中使用警報 function 你可以在這里學習。 按鈕按下示例:
<html> <head> <script> function onError() { try {... } catch (...) { var error error = "button was pressed" alert(error) } } </script> </head> <body> <button id="myButton" onClick="onError()">do not click</button> </body> </html>
<html>
<head runat="server">
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge;" /><%-- Also Check Compatible View --%>
<%-- Put all your jQuery... Here I'm showing an example--%>
<script src="//ajax.microsoft.com/ajax/jQuery/jquery-2.1.1.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
window.setTimeout(function() {
$(".alert").fadeTo(1500, 0).slideUp(500, function() {
$(this).remove();
});
}, 3000);
});
</script>
<form id="form1" runat="server">
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<div runat="server" visible="false" id="AlertDanger" class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert">×</a> <strong>You must have at
least one employee to process</strong>
</div>
</form>
</body>
</html>
注意:-我想你錯過了這個......把你的 jQuery 代碼放在你的身體標簽里......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.