C# CustomValidator ServerValidate 方法不会触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10172406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:44:05  来源:igfitidea点击:

CustomValidator ServerValidate method does not fire

c#asp.netcustomvalidator

提问by Mikayil Abdullayev

I've put a CustomValidatoron my form. I have not set its ControlToValidateproperty. In its ServerValidateevent I've written the following:

CustomValidator在表格上放了一个。我没有设置它的ControlToValidate属性。在它的ServerValidate事件中,我写了以下内容:

protected void CustomValidator1_ServerValidate(object source,      
                                               ServerValidateEventArgs args)
{
    args.IsValid = false;
}

I put a breakpoint to this method but it seems to never come to that point. But if I do this on another form it works like a charm.

我给这个方法设置了一个断点,但它似乎永远不会到达那个点。但如果我以另一种形式这样做,它就像一种魅力。

  1. The ValidationGroupproperty of both the button and the CustomValidatorare the same
  2. I tried deleting this property in both the button and the CustomValidator, still does not work.
  1. ValidationGroup按钮和按钮的属性CustomValidator是一样的
  2. 我尝试在按钮和 中删除此属性,但CustomValidator仍然无效。

It seems as there's something formwide. I just put a CustomValidatoron the form and do not touch any of its properties other than just setting its ServerValidateevent method.

似乎有某种形式的东西。我只是CustomValidator在表单上放了一个,除了设置它的ServerValidate事件方法外,不触及它的任何属性。

EDIT: Here's the aspx part:

编辑:这是 aspx 部分:

 <asp:CustomValidator ID="CustomValidator2" runat="server" 
       ErrorMessage="This is a test" 
   onservervalidate="CustomValidator1_ServerValidate" 
   ValidationGroup="PA"></asp:CustomValidator>


<asp:Button ID="btnPensionersOK" runat="server" Text="OK" Width="75px" 
          onclick="Button1_Click" ValidationGroup="PA" />

采纳答案by Tim Schmelter

Try to force the validation in the button-click handler via Page.Validate:

尝试通过Page.Validate以下方式在按钮单击处理程序中强制验证:

protected void Button1_Click(Object sender, EventArgs e)
{
    Page.Validate();
    if(Page.IsValid)
    {
       // servervalidate should have been called
    }
}

Edit(from comments):

编辑(来自评论):

If you want the customvalidator to validate if nothing was entered/selected in your controls, you need to set ValidateEmptyTextto true. You also might want to let the CustomValidatorreplace the RequiredFieldValidators.

如果您希望 customvalidator 验证您的控件中是否没有输入/选择任何内容,您需要设置ValidateEmptyText为 true。您还可能希望让CustomValidator更换RequiredFieldValidators

I assume that the validator-order on the aspx decides whether or not a customvalidator's severvalidate is called if a previous Validator already has made Page.IsValid=false. Or ASP.NET is so smart that it assumes the SeverValidateto be costlier than a simple text-is-empty check.

我假设 aspx 上的验证器顺序决定是否调用 customvalidator 的 severvalidate 如果以前的验证器已经做了Page.IsValid=false. 或者 ASP.NET 非常聪明,它认为SeverValidate比简单的文本为空检查更昂贵。

回答by Mikayil Abdullayev

I would also like to put some more help for those who will use CustomValidators and RequiredFieldValidators at the same time. One should take into account that Client side validation takes place first. And the server side validation will occur only after PostBack. I'm sure you got it but just in case this is not quite clear: It means first all the controls that are bound to certain client side working validators must be valid to let Postback to occur. After Page. IsValid is True server side stuff takes place and posts back any changes which includes server side validation messages.

我还想为那些同时使用 CustomValidators 和 RequiredFieldValidators 的人提供更多帮助。应该考虑到客户端验证首先发生。并且只有在 PostBack 之后才会进行服务器端验证。我确定你明白了,但以防万一这不是很清楚:这意味着首先绑定到某些客户端工作验证器的所有控件必须有效才能让回发发生。页面之后。IsValid 是真正的服务器端内容发生并回发任何更改,其中包括服务器端验证消息。

So here are the ways one can make both CustomVCalidators and other built in validators to work at the same time.:

因此,以下是使 CustomVCalidators 和其他内置验证器同时工作的方法:

  1. Set both groups of validators to work on Client side. In this case we must ensure that for the custom valitor(s) we spacify the script that will make validation on the client side. Without writing script and just filling in the ServerValidate method the validation will take place in the server.Even if EnableClientScript property is set to True.

  2. Set both groups of validators to work on server side. To do so simply set EnableClientScript to False. But note that this will load the server.

  1. 将两组验证器设置为在客户端工作。在这种情况下,我们必须确保对于自定义 valitor(s),我们将在客户端进行验证的脚本进行压缩。无需编写脚本,只需填写 ServerValidate 方法,验证将在服务器中进行。即使 EnableClientScript 属性设置为 True。

  2. 将两组验证器设置为在服务器端工作。为此,只需将 EnableClientScript 设置为 False。但请注意,这将加载服务器。