473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is this a javascript or asp.net problem?

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
5 1120
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Alex Rast | last post by:
First of all, this is not a programming question. I'm a user, not programming in JavaScript. I'm not, however, a novice user or even a power user - I certainly know programming intimately as well...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.