Results 1 to 21 of 21

Thread: [Resolved] Run java script Ok/Cancel Message Box from command button

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Resolved [Resolved] Run java script Ok/Cancel Message Box from command button

    Lets say I have some java script on my ASP page.
    VB Code:
    1. <input type="submit" name="BtnDelete" value="Delete" id="BtnDelete"
    2.      onclick="return confirm('Are you sure you want to delete?');" />
    Is there a way that I can make this java code run from a VB command button?

    I would also need to know if the user clicked the ok button, or the cancel button.

    If so, what would be the code that would do that?

    Thanks
    Last edited by indydavid32; Oct 20th, 2004 at 07:58 AM.
    David Wilhelm

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    yep it's possible somewhere in your code do something like this
    VB Code:
    1. Me.btnCalculateScores.Attributes.Add("onclick", _
    2.               "return confirm('Are you sure you want to delete?');")
    Then on the confirmation box if the user clicks "Cancel" nothing will happen but if they click "ok" the postback will occur as normal and your VB code will run.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I tried something like that. It does not recognize me.btnDelete. What am I doing wrong?
    David Wilhelm

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Sorry about that must've just been the way i did it at the time, Do it without the me. it should still work the same.

    Presuming of course that you are using a server side button?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Fishcake, that did the trick.

    Now, how could I have a pop up message pop up after the procedure is finished running in the button1.click event?

    Thanks again
    David Wilhelm

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Nevermind, I found the answer.

    Answer
    David Wilhelm

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I ran into another problem here.

    I want the confirm to come up, and if they click yes, while the code is running I want the cursor to change to an hourglass.

    I have java that does this, but it doesn't run if I have the confirm code in there.

    This is what I have.
    VB Code:
    1. <script language="javascript" type="text/javascript">
    2.     function changeCursor(isBusy)
    3.     {document.body.style.cursor = (isBusy == true) ? "wait" : "pointer";}
    4. </script>
    5.  
    6. onclick="return confirm('Are you sure you?');changeCursor(true);"
    With this code, if they hit the button, then click cancel, nothing happens. Which is good.

    If they hit the button, then click on ok, it runs, but the changeCursor function does not run.

    If I switch it to:
    VB Code:
    1. onclick="changeCursor(true);return confirm('Are you sure you?');"
    Then it runs fine when they hit ok, but if they hit cancel, then the changecursor code runs anyway and the cursor changes to the hourglass even though nothing is going on.

    Any ideas?

    Thanks again.
    Last edited by indydavid32; Oct 15th, 2004 at 10:43 AM.
    David Wilhelm

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Code:
    function doit()
    {
         var a = confirm('Are you sure?');
    
         if (a == true)
         {
              changeCursor(true)
         }          
    
    }
    
    onclick="doit();"
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Beautiful! Thanks Cander, works like a charm now.

    I do have a JavaScript book that I am reading now. I will learn as I go.

    I am so glad that I found this web site. You guys have helped me out so many times I feel I somtimes owe you guys part of my salary! lol

    Thanks again.
    David Wilhelm

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    You own me $200 for the answer.


    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Cander, unfortunately, I'm still having a problem. It works fine if I click on ok, the code runs and the cusor changes to the hourglass.

    However, if I click cancel, the code runs anyway.

    Here is my code.
    VB Code:
    1. <script language="javascript" type="text/javascript">
    2.     function changeCursor(isBusy)
    3.     {document.body.style.cursor = (isBusy == true) ? "wait" : "default";}
    4.     function doit()
    5.         {var a = confirm('Are you sure?'); if (a == true) { changeCursor(true) }}
    6. </script>
    7.  
    8. <INPUT id="Button2" style="FONT-SIZE: 5mm; Z-INDEX: 107; LEFT: 8px; WIDTH: 624px; CURSOR: hand; POSITION: absolute; TOP: 184px; HEIGHT: 32px; BACKGROUND-COLOR: silver; TEXT-ALIGN: center; CENTER: 16px"
    9.                 onclick="doit();" tabIndex="7" type="submit" value="IMPORT DTN INFORMATION INTO DATABASE" name="Button2" runat="server">

    Do I need some vb code in my button click event or something?

    Thanks
    David Wilhelm

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    add an else statement and have it return false;

    Code:
    if (a == true) 
    { 
       changeCursor(true) 
    }
    else
    {
       return false;
    }
    Been awhile but that should cancel the onlick event if cancel is clicked
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Still not working for me. The code runs if I click on cancel.
    Code:
    <script language="javascript" type="text/javascript">
    			function doit() 
    				{
    					var a = confirm('Are you sure?'); 
    					if (a == true) 
    					{
    						 ChangeCursor(true)
    					}
    					Else
    					{
    						return false;
    					}
    				}
    			function changeCursor(isBusy)
    				{document.body.style.cursor = (isBusy == true) ? "wait" : "default";}
    		</script>
    David Wilhelm

  14. #14
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    ok scrap the doit function, and just do this for adding that button event in your VB code

    Button1.Attributes.Add("onclick", "var a = confirm('Are you sure?'); if (a == true){ changeCursor(True); }else { return false; } ")
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Cander, this looks like it's closer. When I hit cancel, it does nothing. When I click ok, it runs the code, but the cursor is not changing to the hourglass.
    Here is my function code for the hourglass.
    Code:
    <script language="javascript" type="text/javascript">
    			function changeCursor(isBusy)
    				{document.body.style.cursor = (isBusy == true) ? "wait" : "default";}
    		</script>
    And the VB code for the button.
    VB Code:
    1. If Not IsPostBack = True Then
    2.             Button2.Attributes.Add("onclick", "var a = confirm('Are you sure?'); if (a == true){ changeCursor(True); }else { return false; } ")
    3.         End If
    I very much appreciate your all your help.
    David Wilhelm

  16. #16
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well see the problem is going to be that it submits so fast there is no time for a noticable cursor change. I just dont think there is much you can really do about that.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    That's not the problem. If I just run the cursor function, the hourglass changes. The code that runs takes about 20 seconds. I have other processes that can run for several minutes.

    When I ran the last bit of code you suggested, after I click yes the code runs and while it is running, there is an icon that shows up at the bottom left on the page with what appears to be an exclamation mark with a yellow background. Suggesting to me that there is some sort of asp error.
    David Wilhelm

  18. #18
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    That would be a javascript error. Verify all the javascript being outputted is correct.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I took "changeCursor(true);" out of the code and there is no error. Of course, the ChangeCursor function does not fire, but it wasn't before anyway.

    Maybe it is not possible to fire off a function form a confirm statement. Seems that way to me since both function work indpendantly, but not when put together to run at the same time.

    I don't know, I'm just learning java.
    David Wilhelm

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Hey Cander, I finally got this to work. What worked was I took the braces out from around the function that I was trying to run if the user hits the ok button.
    VB Code:
    1. Button1.Attributes.Add("onclick", "var a = confirm('Are you sure?'); if (a == true) changeCursor(true); else { return false; } ")

    Thank you very much for your help on this.
    David Wilhelm

  21. #21
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Ah ok. Well. There you go.

    You're welcome.

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width