Re: cancelling a postback on a button with javascript

B

Bob

Chris:

Try using the onSubmit event of the Form element to cancel the submission,
as opposed to the onClick event.

<script language=javascript>
function DoSubmit()
{
if (confirm("Sure?"))
{
return true;
}
else
{
return false;
}
}
</script>

<form method=post onSubmit=return DoSubmit();>

Returning false will stop the form from posting.

HTH
 
C

Chris Barrow

Hi Bob,

Unfortunately, the onsubmit method doesn't get invoked when using a
standard postback. For example: Form1.submit(), which is part of the
___dopostack function generated by asp.net doesn't invoke the onsubmit()
method. Too damn bad though, it was a good idea, and I thought I had my
problem solved. Thanks for the help...

Chris...
 
E

Elliot M. Rodriguez

Still solved - change the button from a WebControl to an HTMLInputControl
and specify runat=server. That should do the trick.
 
C

Chris Barrow

Hi Elliot,

It worked! Thank you. I actually had the button declared as an
HTMLInputButton within the vb code-behind, but on the .aspx side as
<input type="button", rather than "submit". Changing the type of button,
caused the "DoSubmit" function to be invoked when the button was
clicked. Pressing the cancel button in the confirm dialog box cancels
the postback.

Thanks again for your help, and sorry for calling you Bob! I must have
been drinking too much mountain dew the first time I replied to you.

Chris...
 
S

S. Justin Gengo

Just as a note, you can stop post back from any type of button if you just
use an if-then javascript for the confirmation:

MyButton.Attributes.Add("onClick", "javascript:if(!confirm('" &
MessageToDisplay & "')) return false;")


--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top