Can I stop the message back using javascript validation?

I have an aspx page with an asp.net button on the page:

<asp:LinkButton ID="btn_Delete" runat="server" OnClick="btn_Delete_Click" OnClientClick="ConfirmDelete();" Text="Delete" /> 

Confirm deletion is as follows:

  function ConfirmDelete() { var answer = confirm("Are you SURE you want to delete this item?"); if (!answer) { return false; } }; 

I assumed that this would prevent the page from being published if the user clicks on cancel, but it still seems to be posting back. Is there a way to prevent sending with confirmation?

+6
source share
1 answer

You must return the value from the function in the event handler:

 OnClientClick="return ConfirmDelete();" 

By the way, you do not need all this logic in the function, just return the result from the confirm call:

 function ConfirmDelete() { return confirm("Are you SURE you want to delete this item?"); }; 
+13
source

Source: https://habr.com/ru/post/891229/


All Articles