Confirm dialogs...really starting to get bloody annoying!!

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Ok,

I have no problem displaying an alert when a button is clicked or at the end of a particular method. I can also call a confirm dialog when the user clicks a particular button, this is based on adding an attribute to the particular button in the page_load event. What I really really want to do is when the user clicks a button is go to the method called by that button and do some of the processing, then display a confirm dialog (i.e. "you have selected 10 people, do you want to give them a raise?", "OK, Cancel") and based on the users action I then either complete that action or abort it.

Now, I have figured a way of calling that confirm dialog in the middle of a method by doing the following:
Code:
1. Add the following to the html code between the body and form tag:
<script>
					<asp:Literal id="ltlAlert" runat="server" EnableViewState="False">
					</asp:Literal>
		</script>

2. Add the ltlAlert to the "Web Form Designer Generated Code" as follows:
Protected WithEvents ltlAlert As System.Web.UI.WebControls.Literal

3. I call the method with the required message:
    Private Sub DisplayMessage(ByVal message As String)
        message = message.Replace("'", "\'")
        message = message.Replace(Convert.ToChar(10), "\n")
        message = message.Replace(Convert.ToChar(13), "")
        ' A confirmation dialog.
        ltlAlert.Text = "confirm('" & message & "')"
    End Sub

Ok, when the method in step 3. is called, it displays the confirm dialog and waits for a response. If i click ok, then the method proceeds. I can click the cancel button till the cows come home, but my action is ignored and the rest of the method is executed.

Any suggestions on how to get around this annoying little problem.

Mike55.
 
I can't quite follow what you're doing, but if the javascript method returns false, then the form shouldn't post back. In otherwords:

<input type="button" value="Click Me" onclick="return false;">

will never cause the form to post.

Sometimes you have to me more squirelly about it:

<input type="button" value="Click Me" onclick="return (if(MyJavaScriptMethod() && MyOtherMethod()){return true;} else {return false;})"

Just make sure whatever attribute is originally calling the method returns false and all should work.
 
bri189a said:
I can't quite follow what you're doing, but if the javascript method returns false, then the form shouldn't post back. In otherwords:

<input type="button" value="Click Me" onclick="return false;">

will never cause the form to post.

Sometimes you have to me more squirelly about it:

<input type="button" value="Click Me" onclick="return (if(MyJavaScriptMethod() && MyOtherMethod()){return true;} else {return false;})"

Just make sure whatever attribute is originally calling the method returns false and all should work.

Thanks for the reply, the code that you supplied works correctly and I am using it else where in my project. But what I want to do is to call the confirm dialog in the middle of a method after the onClick and have the compilier exit the code block and abort execution.

Mike55
 
Let me see if I understand the question...this is what you want:

1. User submits form.
2. Code behind (VB/C#) writes a javascript confirm message (to current page).
3. User clicks Cancel.
4. Code behind stops executing.

If that's what you're trying to do, if I understand you correctly; it's not possible unless you do a multipage type of thing:

1. User submits form.
2. Code behind (VB/C#) writes a javascript confirm message (to current page).
3a. User clicks OK.
4a. Form submits itself and code behind redirects to page (could be same page with query string) that handles 'OK' path.

or

3b. User clicks Cancel.
4b. Form submits itself and code behind redirects to page (could be same page with query string) that handles 'Cancel' path.

ASP and Client browser are a 1 way street - you're either going to the server or coming from the server - you can't stop half way and ask the client to process something and then continue on your original trip to the server. Follow me? Or did I totally miss you're question?
 
I follow completely, appreciate your swift reply. Basically what I was trying to do was to adjust the message appearing in the dialog to suit a number of configuration options.

Mike55.
 
Back
Top