PDA

Click to See Complete Forum and Search --> : [Resolved] Run java script Ok/Cancel Message Box from command button


indydavid32
Oct 11th, 2004, 10:49 AM
Lets say I have some java script on my ASP page.
<input type="submit" name="BtnDelete" value="Delete" id="BtnDelete"
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

Fishcake
Oct 11th, 2004, 12:13 PM
yep it's possible somewhere in your code do something like this
Me.btnCalculateScores.Attributes.Add("onclick", _
"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.

indydavid32
Oct 11th, 2004, 02:38 PM
I tried something like that. It does not recognize me.btnDelete. What am I doing wrong?

Fishcake
Oct 11th, 2004, 04:27 PM
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?

indydavid32
Oct 15th, 2004, 08:31 AM
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

indydavid32
Oct 15th, 2004, 09:17 AM
Nevermind, I found the answer.

Answer (http://www.vbforums.com/showthread.php?s=&threadid=279730&highlight=alert+message)

indydavid32
Oct 15th, 2004, 10:37 AM
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.

<script language="javascript" type="text/javascript">
function changeCursor(isBusy)
{document.body.style.cursor = (isBusy == true) ? "wait" : "pointer";}
</script>

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: 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.

Cander
Oct 18th, 2004, 09:56 AM
function doit()
{
var a = confirm('Are you sure?');

if (a == true)
{
changeCursor(true)
}

}

onclick="doit();"

indydavid32
Oct 18th, 2004, 10:32 AM
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.

Cander
Oct 18th, 2004, 10:33 AM
You own me $200 for the answer.


:afrog:

indydavid32
Oct 18th, 2004, 01:51 PM
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.<script language="javascript" type="text/javascript">
function changeCursor(isBusy)
{document.body.style.cursor = (isBusy == true) ? "wait" : "default";}
function doit()
{var a = confirm('Are you sure?'); if (a == true) { changeCursor(true) }}
</script>

<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"
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

Cander
Oct 18th, 2004, 01:57 PM
add an else statement and have it return false;


if (a == true)
{
changeCursor(true)
}
else
{
return false;
}


Been awhile but that should cancel the onlick event if cancel is clicked

indydavid32
Oct 18th, 2004, 02:29 PM
Still not working for me. The code runs if I click on cancel.
<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>

Cander
Oct 18th, 2004, 02:42 PM
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; } ")

indydavid32
Oct 18th, 2004, 03:11 PM
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.<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.If Not IsPostBack = True Then
Button2.Attributes.Add("onclick", "var a = confirm('Are you sure?'); if (a == true){ changeCursor(True); }else { return false; } ")
End If
I very much appreciate your all your help.

Cander
Oct 18th, 2004, 03:15 PM
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.

indydavid32
Oct 18th, 2004, 03:46 PM
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.

Cander
Oct 19th, 2004, 08:11 AM
That would be a javascript error. Verify all the javascript being outputted is correct.

indydavid32
Oct 19th, 2004, 03:53 PM
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.

indydavid32
Oct 20th, 2004, 08:00 AM
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.
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. :afrog:

Cander
Oct 20th, 2004, 08:05 AM
Ah ok. Well. There you go. :p

You're welcome.

:afrog: