Can I stop an ASP.NET event using jQuery / javascript?

I have the code as shown on the aspx page below:

function CheckEditing() { var answer = confirm('You are currently editing an item. If you continue to the next page your changes will not be saved. Would you like to continue?'); if(!answer) { return false; } return true; } <asp:Button ID="btn_Next" runat="server" Text="Next" onclick="btn_Next_Click" OnClientClick="CheckEditing();" /> 

I thought that returning false would support my asp.net OnClick even from firing, but that's the same. I confirmed that it falls into the return false section of the code using warnings. Is there anything I can do to stop it when starting using jQuery / javascript?

+4
source share
2 answers

Modify OnClientClick as follows:

 OnClientClick="if(!CheckEditing()) return false;" 

Since it displays as if(!CheckEditing()) return false; ASPNetFunction() if(!CheckEditing()) return false; ASPNetFunction() , you will not want to return directly, or the second function will never work.

+4
source

You can also achieve this.

Declare a special validator with the corresponding client validation function

Now, as part of your button, set the validation group to the same as the custom validator

Inside the function CheckEditing () set args.IsValid = false in order not to get into the onClick event

Function CheckEditing (source, arguments) {// ... args.IsValid = false; }

0
source

All Articles