Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > ASP.Net > confirm message box in asp.net


Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2006, 04:23 PM
Beckett Beckett is offline
Regular
 
Join Date: Apr 2005
Location: Frisco, TX
Posts: 83
Default confirm message box in asp.net


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...
Reply With Quote
  #2  
Old 10-11-2006, 04:51 PM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

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...
HTML Code:
<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.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #3  
Old 10-11-2006, 05:06 PM
Beckett Beckett is offline
Regular
 
Join Date: Apr 2005
Location: Frisco, TX
Posts: 83
Default

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>")
Reply With Quote
  #4  
Old 10-12-2006, 08:55 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

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.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #5  
Old 10-12-2006, 10:28 AM
Beckett Beckett is offline
Regular
 
Join Date: Apr 2005
Location: Frisco, TX
Posts: 83
Default

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.
Reply With Quote
  #6  
Old 10-12-2006, 01:07 PM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Quote:
Originally Posted by Beckett
Is this confusing?
Just a little, but those are normally the most fun to answer.
Quote:
Originally Posted by Beckett
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.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #7  
Old 10-24-2006, 11:40 AM
Beckett Beckett is offline
Regular
 
Join Date: Apr 2005
Location: Frisco, TX
Posts: 83
Default

Maybe you are right. I think I will try out a page like you suggested and see how that works. Thanks.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->