ASP.NET MESSAGE BOX

G

Guest

Hi there,
I have the following code which displays a message box on my asp.net page...
i can call this function from a button click or whatever, but the only
problem is that it turns the whole window white and displays the message,
then once you click OK, then the screen goes back to normal... how do i
change it so that the message box just pops up on top of the window and
doesn't turn it all white..???


Public Sub ASPNET_MsgBox(ByVal Message As String)
Try
System.Web.HttpContext.Current.Response.Write("<SCRIPT
LANGUAGE=""JavaScript"">" & vbCrLf)

System.Web.HttpContext.Current.Response.Write("alert(""" &
Message & """)" & vbCrLf)

System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

Catch ex As Exception
End Try

End Sub
 
E

Eliyahu Goldin

That is because you produce the message box before the html body loads.

Put the script on the page and just pass message text from the server like
this:

<script>
var messageText;
function checkMessage(){if (messageText!=null)alert(messageText);}
</script>
<body onload="checkMessage();">

and in code-behind:

Response.Write ("<script>messageText=""Hello world""</script>")

Eliyahu
 
S

Steve C. Orr [MVP, MCSD]

This is because your Response.Write line writes out the alert command very
first thing, so it is executed before the rest of the page is interpreted by
the browser.
You can fix this by outputing the alert line last, or by executing in in the
client side OnLoad event of the page.
Here's more info:
http://steveorr.net/articles/ClientSideSuite.aspx
 
C

Christopher W. Douglas

Adam,

After searching for the same problem myself, and seeing these complicated
responses, I found my own MUCH simpler answer: Add a label to the page,
instead of using response.write.

Create a sub like so:
Private Sub ShowAlert(ByVal Message As String)
Dim MyAlertLabel As New Label
MyAlertLabel.Text = "<script language='vbscript'>" & _
vbNewLine & "Alert(" & """" & Message & """" & ")" & _
vbNewLine & "</script>"
Page.Controls.Add(MyAlertLabel)
End Sub

This will display an alert box without blanking the screen. Adding the new
label did not change the appearance of the page, in the testing I did. It
behaves just like a message box in VB, you can add the ShowAlert("xxx")
within your code wherever you need it, (like in an If statement), it does
not need to be tied to a submit button. For example, you can make it pop up
when the user types in an invalid value on your form.
One suggestion: You do have to make sure to use "VbCrLf" as a newline
character (instead of "VbNewLine") when creating your message, or it will
cause an error on the page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top