button click event not firing if onclientclick event happens first

7 posts, 1 answers
  1. jwhitley
    jwhitley avatar
    41 posts
    Member since:
    Oct 2006

    Posted 30 Mar 2010 Link to this post

    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.
  2. Answer
    Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 31 Mar 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. jwhitley
    jwhitley avatar
    41 posts
    Member since:
    Oct 2006

    Posted 01 Apr 2010 Link to this post

    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'.
  5. Cary
    Cary avatar
    18 posts
    Member since:
    Feb 2008

    Posted 05 Apr 2010 Link to this post

    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.
  6. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012
  7. Michael
    Michael avatar
    26 posts
    Member since:
    Dec 2010

    Posted 16 Mar 2011 Link to this post

    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.
  8. Jaichand
    Jaichand avatar
    24 posts
    Member since:
    Jun 2011

    Posted 14 Jun 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017