Help with Javascript message box in ASP.NET

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello I'm new to ASP.NET and I have problems managing a Javascript "confirm" message box .

Below you will find the public procedure that allows you to get a message box with two options (OK and Cancel) and the code I used to call such a procedure.

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

Sub btnMsgBox_Click(sender As Object, e As EventArgs)
    ShowConfirmMessage("Please choose OK or Cancel.")
End Sub

Of course the code above assumes that you have a button called btnMsgBox.

Now, what I can't do is detect whether the user clicked on OK or Cancel.

Any help?

TIA
 
Hi...This is what I have been using to confirm a delete action:

In the Page_Load() method...add the following like

Me.btnDelete.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete?')")


Private Sub btnDelete_Click(..,..) Handles btnDelete.Click
' This method is fired only when the user clicks OK in the confirm dialogue box
End Sub

Here you dont need to detect what the user clicked. It is handled by "return". If the user clicked OK, the method on the event of the button click is fired and the server side code is executed. Otherwise, it does nothing.

Hope this helps.
SJ
 
Thanks, your code works perfectly.

Unfortunately, I have a problem that does not make it meet my needs.

In particular, your code shows a message box as soon as the button is clicked. On the contrary, I need to execute some code before prompting the user.

For this reason, resorting to an Attributes.Add method may not be as efficient as using the function I posted up in my previous message. Provided that I understand how to intercept the user's click...

sj1187534 said:
Hi...This is what I have been using to confirm a delete action:

In the Page_Load() method...add the following like

Me.btnDelete.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete?')")


Private Sub btnDelete_Click(..,..) Handles btnDelete.Click
' This method is fired only when the user clicks OK in the confirm dialogue box
End Sub

Here you dont need to detect what the user clicked. It is handled by "return". If the user clicked OK, the method on the event of the button click is fired and the server side code is executed. Otherwise, it does nothing.

Hope this helps.
SJ
 
I would like to explain more clearly what my problem is. What I'd like to do is read the value from a textbox in which the user typed something and, only if this value is a certain one, I would ask the user to confirm what he wrote.

This means that clicking on the button should only give a confirm message box if the user inserted a particular value.

Unfortunately, if I adopted the "Attributes.Add" solution, I would have a message box in all cases.

Do you think it is possible to modify/integrate the code I proposed in my first post in order to make it do what I want?

Please consider that with my code you get a confirm message box without any problems. Again, the only thing I can't do is intercept whether the user selects OK or Cancel.

Any help?
 
Back
Top