The World’s Leading Microsoft .NET Magazine
   
 
timstall

Donate Today!

Search Box

 

Calendar

««Sep 2010»»
SMTWTFS
    1234
567891011
12131415161718
19202122232425
2627282930

My RSS Feeds








Mailing List

Most Popular Tags

                                                           

Making a MessageBox in JavaScript

posted Tuesday, 22 February 2005
This is classic - I lose track of how often I see people asking the practical question "How do I create a MessageBox in ASP.Net?" The basic problem is that MessageBoxes occur at the client-side, whereas ASP.Net runs on the server. Therefore you need to have ASP.Net call client-side script, like JavaScript, in order to create the MessageBox.

I had the opportunity to explain this in detail on an article at 4guysfromrolla, at:  http://aspnet.4guysfromrolla.com/articles/021104-1.aspx. Although that article was a year ago, I still get regular emails about parts of it. Most people wanted to know how to have a button give a Yes/No prompt, and then trigger the appropriate server event. Eventually to simplify it I wrote a custom-server control and uploaded it to the control gallery on www.asp.net, at: http://www.asp.net/ControlGallery/ControlDetail.aspx?Control=2071&tabindex=2. It's a free control, with a link to a demo.

[Update]: I posted the source code for this control:

The gist of it is you:

  1. Have ASP.Net create the JavaScript confirm or prompt using either Me.BtnDelete.Attributes.Add(...) or Page.RegisterStartupScript
  2. Put a runat=server hidden html control on the WebForm.
  3. Clicking a button on the JavaScript messagebox runs a client-side script that (1) sets the hidden control's value property and then (2) submits the form (to return flow to the server).
  4. The ASP.Net classes can access the hidden control's value property at the server.

Essentially, you go from Server to Client by registering the script, you go from client to server by submitting the form, and you make data accessible to both by storing it in a runat=server hidden control.

links: digg this    technorati    




1. Edwin Davidson left...
Monday, 21 November 2005 2:54 pm

Tim:

http://aspnet.4guysfromrolla.com/articles/021104-1.aspx
Read your fine article at the above URL but was wondering if it could be modified.
I need a confirm box to popup when a certain condition is true. Say I was adding 100 numbers, 1 to 100. When the counter hits, say, 58 I want the popup box to pause execution, jump up and say to the user 'you've added up 58 numbers - do you want to continue ?' and have them click OK or cancel.
The user will not click on a button of any kind to launch this intially. The code just checks to see if a certain condition is true or not and pops up the confirm box (or not). I am writing my web application with Visual Studio and c#.
How possible is this ?
Thank you very much Tim for your fine article.
Edwin Davidson


2. Tim Stall left...
Monday, 21 November 2005 6:09 pm

Hey Edwin, I assume that this condition is on the server? In this case the problem is that the processing is on the server, but the messageBox is on the client. This is not like a WinForm where a MsgBox just pops up and halts the thread. What you would need to do is:

If the condition is met, then

1 - Save the state of your calculations (perhaps in ViewState),

2 - Return to the client,

3 - Have a RegisterStartup script create a JS confirm

4a - If the user clicks cancel, you're done.

4b - If the user clicks okay, then set a hidden field (runat=server) to some value that indicates the user clicked okay, and submit the form

5 - Now back in the codeBehind (at the server), in the page_Load, check that hidden field value. If it equals the value from part 4b, then you know the user wanted to continue calculations and therefore resume your calculations.

This tedious, but it is certainly possible. The concept is you need to interupt your process to get remote information from another machine (the client), and then return back to the server and resume where you left off.

I spell this out in detail on this blog post:

http://timstall.dotnetdevelopersjournal.com/passing_client_data_to_the_serv er.htm

HTH, Tim


3. michael clifford left...
Monday, 5 December 2005 5:03 pm

thanks for your article Adding Client-Side Message Boxes in your ASP.NET Web > Pages, Part 2 , very helpful, the only problem i had was receiving error; > > BC30648: String constants must end with a double quote > and needed to substitute the last line with strScript += "<"&"/script>" and > it worked fine > 'finishes server processing, returns to client. > Dim strScript As String = " " >


4. Tim Stall left...
Wednesday, 7 December 2005 10:06 am

Hey Michael, Glad you enjoyed the article. Where you able to get the download Demo to work? Conceptually what's going on is that we need to create JavaScript to handle the messageBox, and we're concatenating strings to do that. "String constants must end with a double quote" sounds like an error in the string concatenation. However "</script>" is a valid string literal. Are you getting this as a compile-time error? Did you change something from the demo, or have I misunderstood the problem?

Tim


5. CaitNY left...
Friday, 17 March 2006 4:59 pm

I have a question similar to Edwin's. I want to have the confirm box popup based on a condition. However, it will occur on a click event. This is what I'm trying to do. I have a file upload and when the user clicks the upload button, i want to check if the file exists already, and then confirm if they want to overwrite it. If yes, then upload, if not, then don't. I don't know why I'm so stuck on this! Thanks in advance...


6. Tim Stall left...
Monday, 27 March 2006 9:32 am

Hey Caitny. Good question. What you can do is: 1 - When the user selects the file and clicks the update button 2 - post (or callback) to the server, check if the entered file exists from the filePicker 3 - If it does, then register a startup script with the onclick event. This script will have a confirm() 4 - If the user clicks Yes, then do a Form.Submit() which takes you back to the server. 5 - If the user clicks No, then do nothing (i.e. don't FOrm.Submit)

Note that you can do this with postbacks, or callbacks (if it's ASP.NEt 2.0)

HTH, Tim