最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - OnClientClick code preventing validation from working - Stack Overflow

programmeradmin3浏览0评论

I've added code the the OnClientClick property of an asp 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" />

I've added code the the OnClientClick property of an asp 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" />
Share Improve this question edited Jan 19, 2012 at 0:01 MethodMan 18.9k6 gold badges40 silver badges53 bronze badges asked Jan 18, 2012 at 22:54 Ronald McDonaldRonald McDonald 2,91111 gold badges51 silver badges68 bronze badges 2
  • how e you don't have a runat = server check if this is allowed also "_changesMade=false; return true; can you post the JavaScript code for both OnClientClick= expects javaScript in your case if I am not mistaken – MethodMan Commented Jan 18, 2012 at 22:58
  • id do, i think someone edited the question to just show the relevant parts of the code – Ronald McDonald Commented Jan 18, 2012 at 23:01
Add a ment  | 

3 Answers 3

Reset to default 9

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 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.

发布评论

评论列表(0)

  1. 暂无评论