Two little problems with a Javascript alert box

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, I have found some special code that allows you to simulate the VB.NET ShowMessage method in ASP.NET. This code runs as follows:

Code:
Public Sub ShowMessage(ByVal strMessage As String)
    Dim strScript As String
    strScript = "<script language=javascript> " & Environment.NewLine
    strScript = strScript + "alert('" & strMessage & "')"
    strScript = strScript + "<"
    strScript = strScript + "/"
    strScript = strScript + "script>"
    RegisterClientScriptBlock("clientScript", strScript)
End Sub

Sub btnMsgBox_Click(sender As Object, e As EventArgs)
    ShowMessage("This alert box has been created with Javascript code.")
End Sub

What is great in the code above is that you can call the alert box at any time in your routines. This is much better than another solution I found which relies on the "Attributes.Add" method.

The problem with the latter is that, if you add the onClick attribute to the button, the alert box is triggered as soon as the button is clicked. So, there's no way you can prompt the user with the alert box only if a particular condition occurs.

That's why I believe that the above mentioned ShowMessage sub is the best solution for the creation of message boxes in ASP.NET. Unfortunately, there are two little problems that I would like you to help me sort out.

The first is, when I click on the button, all the objects on the page become invisible as soon as the message box appears. After I click on "OK" in the alert box, they appear again! Although this "bug" does not prevent your Web page from working correctly, it is not professional to see, so I would very much like to get rid of it.

The second problem I have encountered is, I can't go to a new line in the message I put in the alert box! In other words, the code below does not work: (please try to believe)

Code:
Public Sub ShowMessage(ByVal strMessage As String)
    'Same code as above...
End Sub

Sub btnMsgBox_Click(sender As Object, e As EventArgs)
    ShowMessage("This is the first line."  & ControlChars.NewLine & "And this should be the second.")
End Sub

Can anybody give me a helping hand with the code above?

TIA
 
get a second line with the backslash and n ie...

"First line\nSecond line"

The reason your page is disappearing is that it's rendering the client message then doing a postback to the server. Also if you want to ask the user for confirmation...
Code:
var name = confirm("Press a button")
if (name == true)
	document.write("You pressed OK")
else
	document.write("You pressed Cancel")
 
Thanks, Robby. So, the first problem (getting a second line) has been sorted out.

Do you know how I can deal with the fact that my page is "rendering the client message then doing a postback to the server"? How could I prevent the controls from disappearing?

TIA

Robby said:
get a second line with the backslash and n ie...

"First line\nSecond line"

The reason your page is disappearing is that it's rendering the client message then doing a postback to the server. Also if you want to ask the user for confirmation...
Code:
var name = confirm("Press a button")
if (name == true)
	document.write("You pressed OK")
else
	document.write("You pressed Cancel")
 
Back
Top