Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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)

 

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

Pasquale Esposito

Perugia - Italy

http://www.geocities.com/espositosoftware

  • Moderators
Posted

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...

var name = confirm("Press a button")
if (name == true)
document.write("You pressed OK")
else
document.write("You pressed Cancel")

Visit...Bassic Software
Posted

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

 

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...

var name = confirm("Press a button")
if (name == true)
document.write("You pressed OK")
else
document.write("You pressed Cancel")

Pasquale Esposito

Perugia - Italy

http://www.geocities.com/espositosoftware

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...