简体   繁体   中英

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?

Change your OnClientClick like this:

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

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

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM