How to cancel http request from javascript

G

Guest

Hi,

I'm not sure if the title is correct. Here's what I am having trouble with:
I have a server side button control where I am calling a javascript function
via the OnClientClick property. I also have a server side event handler to
handle the On_Click event on the postback. I wish to cancel the postback
based on whether the javascript function returns true or false. The problem
is that the I never get a postback and ofcourse the On_Click event never
fires. Here is my js:

function ConfirmSubmission()
{
return confirm("Do you want to continue?");
}

I set the button OnClientClick property as follows:
OnClientClick="ConfirmSubmission(); And I do have runat server to true.

How can I get this to work? Thanks in advance.
 
R

Riki

Opa said:
Hi,

I'm not sure if the title is correct. Here's what I am having
trouble with: I have a server side button control where I am calling
a javascript function via the OnClientClick property. I also have a
server side event handler to handle the On_Click event on the
postback. I wish to cancel the postback based on whether the
javascript function returns true or false. The problem is that the I
never get a postback and ofcourse the On_Click event never fires.
Here is my js:

function ConfirmSubmission()
{
return confirm("Do you want to continue?");
}

I set the button OnClientClick property as follows:
OnClientClick="ConfirmSubmission(); And I do have runat server to
true.

How can I get this to work? Thanks in advance.

It should be:
OnClientClick="return ConfirmSubmission();"

The word "return" is the key word.
 
G

Guest

Thanks for the reply. I tried this, but the postback is never issue (i.e.
the server side OnClick event is never fired)
What's interesting is if I set the OnClientClick property of the button to
"return false;" or "return true;", I never get a postback. If I leave this
blank. I do get a postback. Shouldn't the "return true" cause the button to
proceed to a postback
or having any return value will stop the postback? The bottom line is I
want to control whether the postback is fired based on the return value of a
javascript call.
How can this be done?
 
M

Mark Rae

Does this work for you?

<asp:Button ID="cmdDelete" runat="server" OnClick="cmdDelete_Click"
OnClientClick="return confirm('Do you want to continue?');" Text="Delete" />
 
G

Guest

Mark, I had the UseSubmitBehavior="False" when I changed it to true, the
logic works as expected. However, in a related question, I don't want any
action when the user hits the enter key, so I suppose I have to trap the
keypresses.

Thanks for the help.
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top