简体   繁体   中英

OnClientClick code preventing validation from working

I've added code the the OnClientClick property of an asp.net button and now the validation controls are not working. When I remove the OnClientClick code the validation works.

How can I get both of them to work?

<asp:Button 
ID="btnNext" 
runat="server" 
Text="Save & Continue" 
SkinID="btnContinue" 
OnClick="btnNext_Click"  
OnClientClick="_changesMade=false; return true;" 
ValidationGroup="vgPreMedSchool" Width="176px" />

Try this.

<asp:Button 
ID="btnNext" 
runat="server" 
Text="Save & Continue" 
SkinID="btnContinue" 
OnClick="btnNext_Click"  
OnClientClick="_changesMade=false; return Page_ClientValidate();" 
ValidationGroup="vgPreMedSchool" Width="176px" />

Page_ClientValidate() is a javascript function that asp.net uses to invoke the page validation.

The validator controls work by default on the client end, essentially adding their own functionality to click click events. There are a couple of ways to solving this:

1) Use custom validators and define your own client validation method. That way you can handle both the validation and the additional processing you need.

2) Disable client side validation. This will allow your client side code to function as needed, and the validation will occur during the post back to the server. However, if you are assuming your data is valid in your own client click method, this won't work for you.

I think you can't call both OnClick and OnClientClick Properties.

You can use only 1 Property at a time.

Thanks.

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