gpt4 book ai didi

asp.net - 如何让服务器端ASP页面在客户端浏览器中显示弹出消息?

转载 作者:行者123 更新时间:2023-12-04 16:31:43 24 4
gpt4 key购买 nike

好的。我已经阅读了很多关于您不能从服务器端 ASP 页面使用 MessageBox.Show 的文章。说得通。那些文章提倡使用“警报”来弹出消息(就像确认消息,用户必须单击“确定”才能确认消息)。一些帖子谈到了注册代码,但其他帖子没有。我已经尝试了我能找到的所有组合,但我仍然无法让我的服务器端 ASP 页面在我的客户端浏览器中弹出一条消息!

这是我的代码隐藏页面中的代码片段:

private void MessageBoxShow(Page page, string message)
{
Literal ltr = new Literal();
ltr.Text = @"<script type='text/javascript'> alert('" + message + "') </script>";
page.Controls.Add(ltr);
}

我也尝试过这种变化:
protected void MyTrace(string msg)
{
Response.Write("<script>alert('" + msg + "')</script>");
}

如果我要去本地主机,这两个都按预期工作,但是当我将代码放在服务器上(在 IIS 7.5 下)时,消息永远不会出现。

有人可以给我一个简单(但完整)的答案吗?谢谢。

最佳答案

我发现警告框很烦人。只需使用 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;
}
}

关于asp.net - 如何让服务器端ASP页面在客户端浏览器中显示弹出消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16287515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com