我发现警报框很烦人。只需使用 HTML 创建您自己的:
<div runat="server" id="AlertBox" class="alertBox" Visible="false">
<div runat="server" id="AlertBoxMessage"></div>
<button onclick="closeAlert.call(this, event)">Ok</button>
</div>
在代码中显示它:
private void MessageBoxShow(string message)
{
this.AlertBoxMessage.InnerText = message;
this.AlertBox.Visible = true;
}
添加一些 CSS 以将其设置为弹出窗口的样式,并添加一些 JavaScript 以在单击“确定”时将其关闭。这是一个完整的演示:
<%@ Page Language="C#" CodeFile="Demo.aspx.cs" Inherits="Demo" %>
<!doctype html>
<html>
<head runat="server">
<meta charset="UTF-8" />
<title>Message Box Demo</title>
<style type="text/css">
.alertBox
{
position: absolute;
top: 100px;
left: 50%;
width: 500px;
margin-left: -250px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
padding: 4px 8px;
}
</style>
<script type="text/javascript">
function closeAlert(e)
{
e.preventDefault();
this.parentNode.style.display = "none";
}
</script>
</head>
<body>
<form runat="server">
<div>
[Regular page content here]
</div>
<div runat="server" id="AlertBox" class="alertBox" Visible="false">
<div runat="server" id="AlertBoxMessage"></div>
<button onclick="closeAlert.call(this, event)">Ok</button>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;
public partial class MessageBoxDemo : Page
{
protected void Page_Load(object sender, EventArgs e)
{
MessageBoxShow("Welcome to my website!");
}
private void MessageBoxShow(string message)
{
this.AlertBoxMessage.InnerText = message;
this.AlertBox.Visible = true;
}
}