Ok. I have read dozens of articles on the fact that you can't use MessageBox.Show from a server-side ASP page. Makes sense. Those articles were advocating using "alert" to pop up a message (like a confirmation message, where the user has to click "OK" to acknowledge the message). Some postings talked about registering the code, but others didn't. I have tried all of the combinations I could find, but I still can't get my server-side ASP page to pop up a message in my client's browser!
Here's a snippet of the code in my code-behind page:
private void MessageBoxShow(Page page, string message)
{
Literal ltr = new Literal();
ltr.Text = @"<script type='text/javascript'> alert('" + message + "') </script>";
page.Controls.Add(ltr);
}
I have also tried this variation:
protected void MyTrace(string msg)
{
Response.Write("<script>alert('" + msg + "')</script>");
}
Both of these work as expected if I'm going to localhost, but when I put my code on the server (under IIS 7.5) the messages never appear.
Can someone please give me a simple (but complete) answer that works? Thanks.
I find alert boxes to be annoying. Just create your own with 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>
Show it in code:
private void MessageBoxShow(string message)
{
this.AlertBoxMessage.InnerText = message;
this.AlertBox.Visible = true;
}
Add some CSS to style it as a popup and some JavaScript to close it when "Ok" is clicked. Here is a completed demo:
<%@ 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;
}
}
Thanks to everyone for their input. Turns out that most of the ideas were workable. My problem was in not realizing that the alert box didn't pop up until after the method was finished (makes sense in hindsight). I ended up using the simplest of the ideas, viz.,
Response.Write("<script>alert('" + msg + "')</script>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With