Is this a javascript or asp.net problem?


P

phil

Hi,

Is this a javascript or asp.net problem?
When the button is clicked, the server event in the code-behind must be
executed if the user clicked on "OK" of the Confirm and not executed if
clicked on "Cancel". I tried two ways: the first here below works perfect
but the second way ALWAYS executes the server event!!

Can anybody explain me why?
Thanks
Phil

First way (this works)
--------------------
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" runat="server"
onclick="return confirm('are you sure?');" />
</form>

Second way (server event is always executed)
----------------------------------------------
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" runat="server"
onclick="hfd();" />
</form>
<script language="javascript" type="text/javascript">
function hfd()
{return confirm("are you sure?")}
</script>

Code-behind (vb.net):
 
Ad

Advertisements

P

Patrice

You forgot to return the function value from the onclick event (that is
"return hfd();" not just "hfd();")
 
P

phil

Hi, thanks for replying ...
You 're right, but in fact it should also work, because the function 'hfd()'
contains the 'return' in its code, no?
I don't understand why it's necessary to add a second time 'return' ...
 
H

Hans Kesting

Hi,
Is this a javascript or asp.net problem?
When the button is clicked, the server event in the code-behind must be
executed if the user clicked on "OK" of the Confirm and not executed if
clicked on "Cancel". I tried two ways: the first here below works perfect
but the second way ALWAYS executes the server event!!

Can anybody explain me why?
Thanks
Phil

First way (this works)
--------------------
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" runat="server"
onclick="return confirm('are you sure?');" />
</form>

Just a remark:
On a LINKbutton this wouldn't work, you would need
onclick="if (!confirm('sure?')) return false;"

asp.net will add it's own (javascript) code to this, so you don't want
to "return" too early (and don't forget that final ;)

Hans Kesting
 
P

Patrice

As any other statement its location is not meaningless. return means "return
the value to the caller".

In the first sample this statement is inside the click handler that is you
return a value to the caller of the click handler.

In the second sample, the return statement is inside your function so this
function return a value to the click handler. But as it is missing from the
click handler, the value is not returned to the caller of the click handler
as done in the first sample...
 
Ad

Advertisements

C

Chris

Thanks both ...


Hans Kesting said:
Just a remark:
On a LINKbutton this wouldn't work, you would need
onclick="if (!confirm('sure?')) return false;"

asp.net will add it's own (javascript) code to this, so you don't want
to "return" too early (and don't forget that final ;)

Hans Kesting
 
Ad

Advertisements


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

Top