JavaScript confirm or radconfirm not triggering the PostBack Event

JavaScript confirm or radconfirm not triggering the PostBack Event, radconfirm

I have a case where I need to add a confirm dialog to the "OnClientClick" event for a WebForms submit button, after adding the confirm dialog, I noticed that the PostBack event of the submit button is not working anymore.

This is the html code that I added to the aspx page, note that the asp.net button is added inside RadAjaxPanel:

<asp:Button ID="btnDelete" runat="server" CssClass="button" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');"  />

The code looks good but when user clicks the delete button and then confirm the deletion, the PostBack event is not being triggered.

In order to solve this issue that is related to the way of propagating the PostBack event in case of AjaxPanel, I had to modify the code in the OnClientClick as per the following:

<asp:Button ID="btnDelete" runat="server" CssClass="button" Text="Delete" OnClientClick="if (!confirm('Are you sure you want to delete this record?')) return false;"  />

This will solve the issue and if users clicks 'Ok' in the conform window then the PostBack event will be triggered.

This is a short tip for developers who might face similar issue.

Post a Comment

Previous Post Next Post