confirm message box in asp.net

Beckett
10-11-2006, 03:23 PM
I need a message box to appear in my asp.net application. Ideally this would have yes/no buttons, but I can live with OK/Cancel buttons if necessary. I have tried the following 2 things to get a confirm message box to appear -- but I don't know how to make something happen if they click Cancel. Is there any way to make something happen if the user clicks Cancel? With the 2nd example below, I can make something happen if they click OK, but I wanted to have something ELSE happen if they clicked Cancel (really like a Yes No button instead).

Also if I want to have 2-line message on the box, does anyone know how to create a line break in the message?

Button1.Attributes.Add("onClick", "return confirm('Do you want to mark this form as complete?');")

Button1.Attributes.Add("onClick", "javascript:if(confirm ('Do you want to mark this form as complete') == false) return false")

p.s. I do not know much about javascript obviously...

wayneph
10-11-2006, 03:51 PM
using the default JavaScript confirm dialog you are stuck with "OK" and "Cancel"

The easiest way to do what you want is to call a function.

Button1.Attributes.Add("onClick", "myFunction();")

And then create your function on your page...
<script type="text/javascript">
function myFunction() {
if confirm('Do you want to mark this form as complete?\nThis is line 2.') {
//put the stuff to do for OK here
}
else {
//Put the stuff for Cancel here
}
}
</script>

To get a line break you can use the \n escape character. (Also in the example above.)

Beckett
10-11-2006, 04:06 PM
Thanks for your response. I just realized I can't have the confirm box open from a button click -- it only should happen conditionally. So the only other way I know how to open a message box is the following code on my code behind page -- can I still call a function as you suggested above from the code below somehow? Or is there a better way I should be doing this?

Dim Message As String
Message = "All questions have been answered. Do you want to mark this form as complete?"
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)

System.Web.HttpContext.Current.Response.Write("confirm(""" & Message & """)" & vbCrLf)

System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

wayneph
10-12-2006, 07:55 AM
Something isn't sounding right... It sounds like you're going to be making a Full extra round trip to the server...

What are you trying to accomplish? Maybe if we start from the beginning, we can come up with another idea.

Beckett
10-12-2006, 09:28 AM
OK, I have a questionnaire that users fill out. One question appears on the screen at a time. They answer the question, and when they click "Next Question" their answer is saved to the database and the questionnaire moves onto the next question. They can also skip a question -- nothing forces them to answer. There are a different number of questions depending on the user's login credentials and the number of questions can change at any point if the System Admin adds more.

So what I need the confirm message box for is -- If the user has answered all questions that were assigned to him/her, I need a prompt to ask if they want to mark the questionnaire as complete. The prompt would come up either when they click NExt Question, Previous Question, or the Save/Exit button - but it would only come up if they have answered all questions and have not already marked the form as completed (by a different "mark complete" button I have). And if they click "No" to the prompt- like they do not want to mark it as complete, they should still be able to use the Next and Previous buttons to scroll through their questionnaire without getting the prompt again.

I need to check the database after every question to see if they've answered them all -- I can't just have the prompt come up at the last question, because they may have skipped a question to go back and answer later, in which case the form should not be marked as complete.

Is this confusing?

thanks.

wayneph
10-12-2006, 12:07 PM
Is this confusing?Just a little, but those are normally the most fun to answer. :p
I need to check the database after every question to see if they've answered them all -- I can't just have the prompt come up at the last question, because they may have skipped a question to go back and answer later, in which case the form should not be marked as complete.
In this case, I don't think I'd use JavaScript at all for this. I would probably create a separate page for asking the user if they are done. Each time you save a question, you can test to see if they're done. If they are, send them to the confirmation page. On there, you can list all of their options, and give them a link to "Complete" or a link to "Review Answers" or what ever other option you want to give them.

You can use a Simple Response.Redirect in your code to send the user to the confirm page.

It sounds a little bit easier than saving the answer, and redisplaying the same page, just to determine if they want to complete. This will give them a full page with all of the possible options at once.

Beckett
10-24-2006, 10:40 AM
Maybe you are right. I think I will try out a page like you suggested and see how that works. Thanks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum