By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,663 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,663 IT Pros & Developers. It's quick & easy.

Is this a javascript or asp.net problem?

P: n/a
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):
-------------
Protected Sub Button1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick
Response.Write("ok from server")
End Sub
Jun 9 '06 #1
Share this Question
Share on Google+
5 Replies


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

--
Patrice

"phil" <pd*@dffgf.ee> a écrit dans le message de news:
uw*************@TK2MSFTNGP05.phx.gbl...
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):
-------------
Protected Sub Button1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick
Response.Write("ok from server")
End Sub

Jun 9 '06 #2

P: n/a
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' ...

"Patrice" <sc****@chez.com> wrote in message
news:Oz**************@TK2MSFTNGP03.phx.gbl...
You forgot to return the function value from the onclick event (that is
"return hfd();" not just "hfd();")

--
Patrice

"phil" <pd*@dffgf.ee> a écrit dans le message de news:
uw*************@TK2MSFTNGP05.phx.gbl...
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):
-------------
Protected Sub Button1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick
Response.Write("ok from server")
End Sub


Jun 9 '06 #3

P: n/a
> 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
Jun 9 '06 #4

P: n/a
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...

--
Patrice

"phil" <pd*@dffgf.ee> a écrit dans le message de news:
uc**************@TK2MSFTNGP05.phx.gbl...
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' ...

"Patrice" <sc****@chez.com> wrote in message
news:Oz**************@TK2MSFTNGP03.phx.gbl...
You forgot to return the function value from the onclick event (that is
"return hfd();" not just "hfd();")

--
Patrice

"phil" <pd*@dffgf.ee> a écrit dans le message de news:
uw*************@TK2MSFTNGP05.phx.gbl...
> 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):
> -------------
> Protected Sub Button1_ServerClick(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.ServerClick
> Response.Write("ok from server")
> End Sub
>
>



Jun 9 '06 #5

P: n/a
Thanks both ...
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:mn***********************@spamgourmet.com...
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

Jun 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.