How to execute click event of an asp.net button based upon the return value of OnClientClick event

I am working on an asp.net web forms project. I have an asp.net button which has both OnClientClick and onClick events. The OnClientClick event executes the javascript function processHolidayDates() which in turn shows a Telerik's Ok/Cancel message box. When the Ok button is clicked it will return true and if the Cancel button is clicked it will return false. So, when false is returned, I don't want the button click event to execute the server side code. However, the moment the message box is displayed already the code behind under the btnCreate_Click is executed. When I add "return false;" at the OnClientClick as in "OnClientClick="javascript:processHolidayDates(); return false " , the code behind is never executed even if I chose Ok in the message box. The following is my code on the aspx page

    <asp:button id="btnCreate" runat="server" Text="Create"  OnClientClick="javascript:processHolidayDates(); " onclick="btnCreate_Click"></asp:button> 
function processStartAndEndDates() {

        var oConfirm = radconfirm('Hello', callBackFn, 400, 300, null, 'Test');
    return oConfirm ;

    }

2 answers

  • answered 2018-03-13 21:30 hardkoded

    This should work:

    OnClientClick="return processHolidayDates();"
    

  • answered 2018-03-13 21:31 Daniel Forslund

    You could change your OnClientClick to

    OnClientClick="return processHolidayDates();"
    

    So that you will return false (and cancel the postback) if the user cancels the window.