This is a migrated thread and some comments may be shown as answers.

button click event not firing if onclientclick event happens first

7 Answers 3441 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
jwhitley
Top achievements
Rank 1
jwhitley asked on 30 Mar 2010, 03:52 PM
I'm performing the rather laborious task of upgrading from asp.net q3 2007 to ajax q1 2010. One of the several issues I've faced so far is my standard asp.net buttons which require a clientside confirmation before the postback is fired.

Example, I have a reasonably simple page with a radscriptmanager, a radajaxmanager and a radgrid. On this page I have an asp.net standard button with events for onclick and onclientclick. The onclientclick is there to make sure the user meant to press the button and is simply "return confirm('Delete this email?');", but even pressing OK causes no postback. If I remove the onclientclick event then the postback happens fine (javascript for client code is in a radcodeblock). If I change the client event to just be 'return true;' the postback still does not happen.

Something about client side javascript (possibly because it's in a radcodeblock?) firing first is stopping the postback from the button happening.

Any ideas?
Thanks.

7 Answers, 1 is accepted

Sort by
1
Accepted
Nikolay Rusev
Telerik team
answered on 31 Mar 2010, 02:01 PM
Hello John,

The behavior which you are observing is due to the fact that server buttons are not submit buttons. The buttons are rendered as <input type="button".. most probably and if you attach client click handler of such a button that returns a value it will prevent execution of the script that are attached after your code(usually this is the script for submission of the page added by the framework).

If you use FireBug or IE Developer toolbar to inspect the code for the rendered button you will see markup like this:
<input type="button" id="Button1" onclick="return call_to_your_function();__doPostBack('Button1','')" value="Postback" name="Button1">
Everything after return call_to_your_function(); won't executed regardless whether your script is returning true.

To overcome this you must change the code for OnClientClick as follow:<asp:Button runat="server" ID="Button1" Text="Postback" OnClientClick="if(!myFunc()) return false;"/>

This way if your code returns true, no return true/false clause will be executed and ajax will be performed.

All the best,
Nikolay
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
jwhitley
Top achievements
Rank 1
answered on 01 Apr 2010, 10:00 AM
Thanks Nikolay,

Have amended the onclientclick code as per your suggestion and it now works. I'm still not completely sure why the change was needed (this was working fine under the Q3 2007 Telerik code [not Promethius]) and the same portions of the page were 'ajaxified'.
0
Cary
Top achievements
Rank 2
answered on 05 Apr 2010, 07:15 PM
I also noticed when upgrading to Q1 2010 that having OnClientClick="return confirm('Are you sure?')" no longer worked and had to replace it with OnClientClick="if(!confirm('Are you sure?'))return false;" My previous version was 2009.3.1103.35 and when I moved to version 2010.1.309.35 I had to change my code as outlined above.. It's not a hard change, in fact based on your post it was probably a good change even if this still worked as this "new" way will work in all situations and should work on all browsers.
0
Jonx
Top achievements
Rank 2
answered on 18 Jan 2011, 01:54 AM
0
Michael
Top achievements
Rank 2
answered on 16 Mar 2011, 11:32 AM
Thank you!

After ajaxifiyng my RadGrid I had to replace all my OnClientClick="return confirm('Are you sure?');" just as you described. Otherwise the Buttons would not postback at all.
0
Jaichand
Top achievements
Rank 1
answered on 14 Jun 2012, 09:36 AM
Thanks Nikolay Rusev
I was facing the same  problem of Onclient click working but Onclick event not firing.
when I use OnClientClick="if(!myFunc()) return false;" my problem get resolved.
0
HrMeibom
Top achievements
Rank 2
Iron
answered on 26 May 2021, 11:21 AM

Thanks again Nikolay!

I found this thread posted 11 years ago and it still solved my problem. Spent several hours trying to figure it out.

+1 vote up to you Nikolay :-)

/JannikM

Tags
Ajax
Asked by
jwhitley
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
jwhitley
Top achievements
Rank 1
Cary
Top achievements
Rank 2
Jonx
Top achievements
Rank 2
Michael
Top achievements
Rank 2
Jaichand
Top achievements
Rank 1
HrMeibom
Top achievements
Rank 2
Iron
Share this question
or