Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display 'Confirm' then HOLT app until ok clicked

Status
Not open for further replies.

apc2003

Programmer
Aug 29, 2003
54
0
0
GB
I'm using the following code:

Code:
<script>confirm('Continue ?');</script>

to display a confirm dialog but the application continues to execute even when this is shown.

How do I holt application execution until OK or cancel has been clicked on the confirm dialog?

Regards in advance...
 
You'll need to return the value of the confirm box. Here's an example:

Code:
<form name="f" onsubmit="return confirm('submit?');">
  <input type="text" name="t" />
  <input type="submit" />
</form>

The code above will show a simple form. When you click "submit" a confirmation box will popup. If you say yes, it will submit, otherwise, nothing will happen.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
This wont work in the code i'm running...

I have a ASP.net app that conditionally shows this confirm dialog if a validate returns false.

if it returns true it is not shown.

if it is shown I need to holt the app until the user oks or cancels.

Any suggestions welcome...

Regards in advance...
 
Try
Code:
var retval = confirm("Continue?");
If you are setting a variable to the result, it may wait until the value has a value. If that doesn't work, try
Code:
var confirmed = "n";
while (confirmed == "n");
  confirmed = confirm("Continue?");
This should execute the [tt]while[/tt] loop while confirmed is still "n".

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
You also posted this in the ASP.NET forum, with the same objection to the same question. We're telling you the right way to do it. Perhaps we just don't understand your code.

Can you post a stripped down version of your HTML and your ASP.NET Code Behind?

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
I have a save button that:

1. Runs a validate method that returns true or false

2. If validate returns true the save method continues and does its work.

3. If validate returns false I need to show a Confirm Dialog so that the user can choose to continue or cancel.

It is at stage 3 that the Confirm must show and holt the application, then cancel the save method if the user clicks cancel or allow save to continue if the user clicks ok.

Ragards in advance...
 
Is this something like what you need?

Code:
<html>
<head>
<title>test page</title>

<script type="text/javascript">
function doValidate() {
	var isValid;

	//do your form validation here
	//I am simply checking the user has entered something in the text box
	if(document.forms[0].txt1.value == '') {
		isValid = false;
	} else {
		//all validation ok
		isValid = true;
	}

	return isValid;
}

function doSave() {
	//do validation
	if(doValidate()) {
		//all ok
		return true;
	} else {
		//not valid
		//get user to confirm continue
		return confirm('You have not entered a name.  Do you want to continue?');
	}
}
</script>

</head>

<body>
<form action="test.html" method="post" onsubmit="return doSave();">
<input type="text" name="txt1" />
<input type="submit" value="Save" />
</form>

</body>
</html>

--James
 
It seems to me that you misunderstand when and where code runs. Server-side vs. Client-side.

The problem with ASP.NET Validation "controls" is that you can't be quite sure where it is going to run. I suggest you write your own validation in JavaScript.

That way, you can move your "Save" button "click" event from the server, to the client.

Your program flow would be:

1) Server-side code renders a page to the browser.

2) User clicks "Save", which calls a client-side JavaScript function.

3) The function validates the form. If it passes validation, it does a form.submit() to return control to the server. You put your code in the Page_Load event, checking for postbacks, etc.

4) If, still on the client, the page doesn't pass validation, show a JavaScript confirm(). If the user selects "ok", submit the form. If not, "return false" to cancel any form submission.

This is the RIGHT WAY to do this, believe me! At some point, if you're lost and seeking advice, you have to take the advice you're being given.


Consider the alternative:

1) Server renders a page

2) User clicks "Save", which submits back to the server.

3) Server validates

4) Validation fails, so server must render/register script to browser

5) Client runs script, shows Confirm(), user confirms,

6) Server processes the form AGAIN, this time skipping validation.

Way to much back-and-forth.






Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
I don't mean to be rude so please don't take it that way... I appreciate all the help. :)

I do understand how Server-side vs. Client-side code executes / runs.

OK... :) Here is the full scenario and what I need to accomplish!

I have a Save button in C# side of the asp.net web application.

Before the Save does its work against a SQL Database I run a method that does some checks and returns true or false.

Based on this return value if it is false I need to show a dialog asking the user to continue or cancel the save, this is the point where I need to hold code execution.

If it returns true the save should carry on without showing the dialog.

I don't think it can be done using the HTML side of the application.

I need to fire the code based on the return value of my check method. If the Dialog is shown I need to stop the code execution and catch the user input, ok or cancel, and based on this I need to take action, continue with save or cancel the save.

Thanks again for any and all help given... :)
 
I understand. As the server is running code, you need to stop and ask the user something before you proceed. Right?

You can't really do that. You have to pass control from the server back to the browser, run some code there, and then pass control back to the server. You can't really define an entry point, so you can jump right back to where you were.

So you'll have to structure it this way:

1) Run your Save/event just as you are.

2) Run your validate there, and branch to either:

2A) validation passes, call a method to do your DB commits, or

2B) write a JavaScript "confirm" script back to the browser, as well as setting some hidden or session state variable, AND END THE METHOD THERE.

3) IF the user never comes back (because the script did a "return false" and so never re-submitted, well... you're done.

4) IF the user clicks "OK", you'll be taken to your server Page_Load method/event. Check the value of that variable you set/created in step 2B. That will tell you "the user submitted once, failed validation, but submitted again. At that point, run your DB commit method.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top