|
-
Oct 11th, 2004, 10:49 AM
#1
Thread Starter
Fanatic Member
[Resolved] Run java script Ok/Cancel Message Box from command button
Lets say I have some java script on my ASP page.
VB Code:
<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
Last edited by indydavid32; Oct 20th, 2004 at 07:58 AM.
David Wilhelm
-
Oct 11th, 2004, 12:13 PM
#2
yep it's possible somewhere in your code do something like this
VB Code:
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.
-
Oct 11th, 2004, 02:38 PM
#3
Thread Starter
Fanatic Member
I tried something like that. It does not recognize me.btnDelete. What am I doing wrong?
-
Oct 11th, 2004, 04:27 PM
#4
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?
-
Oct 15th, 2004, 08:31 AM
#5
Thread Starter
Fanatic Member
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
-
Oct 15th, 2004, 09:17 AM
#6
Thread Starter
Fanatic Member
Nevermind, I found the answer.
Answer
-
Oct 15th, 2004, 10:37 AM
#7
Thread Starter
Fanatic Member
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:
<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:
VB Code:
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
-
Oct 18th, 2004, 09:56 AM
#8
Code:
function doit()
{
var a = confirm('Are you sure?');
if (a == true)
{
changeCursor(true)
}
}
onclick="doit();"
-
Oct 18th, 2004, 10:32 AM
#9
Thread Starter
Fanatic Member
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.
-
Oct 18th, 2004, 10:33 AM
#10
You own me $200 for the answer.
-
Oct 18th, 2004, 01:51 PM
#11
Thread Starter
Fanatic Member
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:
<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
-
Oct 18th, 2004, 01:57 PM
#12
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
-
Oct 18th, 2004, 02:29 PM
#13
Thread Starter
Fanatic Member
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>
-
Oct 18th, 2004, 02:42 PM
#14
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; } ")
-
Oct 18th, 2004, 03:11 PM
#15
Thread Starter
Fanatic Member
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:
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.
-
Oct 18th, 2004, 03:15 PM
#16
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.
-
Oct 18th, 2004, 03:46 PM
#17
Thread Starter
Fanatic Member
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.
-
Oct 19th, 2004, 08:11 AM
#18
That would be a javascript error. Verify all the javascript being outputted is correct.
-
Oct 19th, 2004, 03:53 PM
#19
Thread Starter
Fanatic Member
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.
-
Oct 20th, 2004, 08:00 AM
#20
Thread Starter
Fanatic Member
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:
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.
-
Oct 20th, 2004, 08:05 AM
#21
Ah ok. Well. There you go. 
You're welcome.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|