ASP.NET Web 表单自定义验证器未触发
我的页面上有一个用于文件上传控件的自定义验证器。
<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>
然后我还有一个按钮。
<asp:Button ID="btnSubmit" ValidationGroup="EditorValidate" OnClick="btnSubmit_Click" runat="server" Text="Add Vendor Brief" />
我已经像这样定义了我的自定义验证器事件...
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");
}
这个自定义验证器甚至没有被解雇。在我看来一切都很好。如果我在服务器验证事件中的任何位置放置断点,那么当我单击“提交”时,它不会被命中。不过,我可以在提交按钮的单击事件中命中断点。
有什么想法吗?
编辑 - 我在页面上有其他验证控件,例如必填字段验证器,并且它们运行得很好。
编辑2 - 如果您想要页面及其代码隐藏的完整源代码,请点击以下链接:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要为按钮和验证器指定相同的 ValidationGroup=""
You need to specify the same ValidationGroup="" to your button, and to your validators
对我来说,当验证器及其相关输入位于在控件标记中设置了
visible="false"
的控件内时,就会发生这种情况。这导致 CustomValidator 继承Visible = false
属性并阻止验证触发。在正常的页面加载中,直到页面生命周期的后期我才使控件可见。无论如何,如果您在
Page.Validate()
方法上设置断点,您可以检查Page.Validators
集合,看看是否会发生类似的情况。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. This was causing the CustomValidator to inherit theVisible = false
property and preventing validation from firing. 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 thePage.Validators
collection and see if a similar thing might be happening to you.将
CausesValidation="True"
添加到您的 Button 声明中。Add
CausesValidation="True"
to your Button declaration.如果您查看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
你看
那么,是否正在测试页面加载中的
Page.IsValid
?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
you see
So, are in testing for
Page.IsValid
in your Page Load?尝试完全删除
ControlToValidate
。尽管我以前从未尝试过验证文件上传,但如果内容为空,大多数验证器都不会触发(RequiredField
除外)。取消控制进行验证应使其始终为该组触发。编辑 (Chevex) -
ControlToValidate
是问题所在,但并不是因为它被破坏了。默认情况下,它不会在没有值的控件上触发,如上所述。设置自定义验证器控件属性ValidateEmptyText="true"
可以解决该问题。遗憾的是我不得不开始这个巨大的问题只是为了找到这一点,但现在我们知道了! :)Try removing
ControlToValidate
entirely. Though I've never tried to validate a file upload before, most validators won't fire (exceptRequiredField
) if the contents are empty. 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. By default it will not fire on controls with no value, as stated above. Setting the custom validator control propertyValidateEmptyText="true"
solves the issue. Sad that I had to start this giant question just to find that, but now we know! :)