简体   繁体   English

ASP.NET Web 表单自定义验证器未触发

[英]ASP.NET Web-forms custom validator not firing

I have a custom validator on my page for a file upload control.我的页面上有一个用于文件上传控件的自定义验证器。

<asp:FileUpload ID="fuVendorBrief" runat="server" />
<br />
<asp:CustomValidator ID="cvVendorBriefFile" Display="Dynamic" runat="server" ValidationGroup="EditorValidate" ControlToValidate="fuVendorBrief" OnServerValidate="cvVendorBriefFile_ServerValidate" ErrorMessage="You must upload a vendor brief PDF file.">     
</asp:CustomValidator>

I then also have a button.然后我也有一个按钮。

<asp:Button ID="btnSubmit" ValidationGroup="EditorValidate" OnClick="btnSubmit_Click" runat="server" Text="Add Vendor Brief" />

I have defined my custom validator event like so...我已经像这样定义了我的自定义验证器事件......

protected void cvVendorBriefFile_ServerValidate(object source, ServerValidateEventArgs args)
{
    CustomValidator fileUploadValidator = (CustomValidator)source;
    FileUpload vendorBriefFileUpload = (FileUpload)fileUploadValidator.Parent.FindControl(fileUploadValidator.ControlToValidate);
    args.IsValid = vendorBriefFileUpload.HasFile && vendorBriefFileUpload.FileName.ToLower().EndsWith(".pdf");
}

This custom validator isn't even getting fired.这个自定义验证器甚至没有被解雇。 Everything looks alright to me.对我来说一切都很好。 If I drop a breakpoint anywhere in the server validation event it does not get hit when I click submit.如果我在服务器验证事件中的任何位置放置断点,当我单击提交时它不会被命中。 I can hit breakpoints in the submit button's click event however.但是,我可以在提交按钮的点击事件中点击断点。

Any ideas?有任何想法吗?

EDIT - I have other validation controls, like required field validators, on the page and they fire just fine.编辑- 我在页面上有其他验证控件,例如必需的字段验证器,它们可以正常触发。

EDIT 2 - If you want the full source of the page and its codebehind then follow these links:编辑 2 - 如果您想要页面的完整源代码及其代码隐藏,请点击以下链接:

Try removing ControlToValidate entirely.尝试完全删除ControlToValidate Though I've never tried to validate a file upload before, most validators won't fire (except RequiredField ) if the contents are empty.尽管我以前从未尝试过验证文件上传,但如果内容为空,大多数验证器都不会触发( RequiredField除外)。 Taking off the control to validate should make it fire always for that group.取消对验证的控制应该使它始终为该组触发。

EDIT (Chevex) - The ControlToValidate was the issue, but not because it was broken.编辑(Chevex)- ControlToValidate是问题,但不是因为它坏了。 By default it will not fire on controls with no value, as stated above.默认情况下,它不会触发没有值的控件,如上所述。 Setting the custom validator control property ValidateEmptyText="true" solves the issue.设置自定义验证器控件属性ValidateEmptyText="true"可以解决该问题。 Sad that I had to start this giant question just to find that, but now we know!很遗憾我不得不开始这个巨大的问题才能找到它,但现在我们知道了! :) :)

For me this occurred when the validator and its related input were inside a control that had visible="false" set in the control mark up.对我来说,当验证器及其相关输入位于控件标记中设置了visible="false"的控件内时,就会发生这种情况。 This was causing the CustomValidator to inherit the Visible = false property and preventing validation from firing.这导致 CustomValidator 继承Visible = false属性并阻止验证触发。 On a normal page load I wasn't making the controls visible until later in the page lifecycle.在正常的页面加载中,我直到页面生命周期的后期才使控件可见。

In any case if you set a breakpoint on the Page.Validate() method you can inspect the Page.Validators collection and see if a similar thing might be happening to you.在任何情况下,如果您在Page.Validate()方法上设置断点,您可以检查Page.Validators集合,看看是否可能发生类似的事情。

您需要为按钮和验证器指定相同的 ValidationGroup=""

CausesValidation="True"添加到您的 Button 声明中。

If you look to the documentation at http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(%22ASP%3aCUSTOMVALIDATOR%22);k(VS.HTMLDESIGNER.HTML);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-ASPX)&rd=true如果您查看http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(%22ASP%3aCUSTOMVALIDATOR%22);k(VS.HTMLDESIGNER.HTML);上的文档k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-ASPX)&rd=true

you see你看

When using validator controls, you should always check the results of server-side validation before performing any processing.使用验证器控件时,应始终在执行任何处理之前检查服务器端验证的结果。 After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the Page.IsValid property.在回发之后但在调用事件方法之前,页面调用验证器控件并将它们的结果聚合到 Page.IsValid 属性中。 (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the Page.IsValid property returns true before processing input. (您也可以使用 Validate 方法显式调用验证器控件。)在您自己的代码中,您应该在处理输入之前检查 Page.IsValid 属性是否返回 true。 Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check Page.IsValid in server code before processing validated data.如果验证检查失败,即使启用脚本的浏览器可能会阻止在客户端上发生回发,您也应该始终在处理验证数据之前检查服务器代码中的 Page.IsValid。

So, are in testing for Page.IsValid in your Page Load?那么,是否在测试页面加载中的Page.IsValid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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