Abe Miessler
Abe Miessler

Reputation: 85046

Possible to stop ASP.NET event using jQuery/javascript?

I have some code like below in an aspx page:

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 had thought that returning false would keep my asp.net OnClick even from firing, but it still does. I've confirmed that it is getting to the return false section of code using alerts. Is there anything I can do to stop it from firing using jQuery/javascript?

Upvotes: 4

Views: 1602

Answers (2)

Kushal Vaghani
Kushal Vaghani

Reputation: 11

You can achieve this also, in this way.

Declare a custom validator with the appropriate client validation function

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

Inside the function CheckEditing(), set args.IsValid = false for not going onto the onClick event

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

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

Change your OnClientClick like this:

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

Because it renders as if(!CheckEditing()) return false; ASPNetFunction() you wouldn't want to return outright, or the second function would never run.

Upvotes: 4

Related Questions