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

发布于 2024-11-01 19:52:15 字数 1490 浏览 0 评论 0原文

我的页面上有一个用于文件上传控件的自定义验证器。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤单情人 2024-11-08 19:52:16

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

You need to specify the same ValidationGroup="" to your button, and to your validators

弥繁 2024-11-08 19:52:16

对我来说,当验证器及其相关输入位于在控件标记中设置了 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 the Visible = 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 the Page.Validators collection and see if a similar thing might be happening to you.

习ぎ惯性依靠 2024-11-08 19:52:16

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

Add CausesValidation="True" to your Button declaration.

﹎☆浅夏丿初晴 2024-11-08 19:52:16

如果您查看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
财产。 (您也可以致电
验证器控制显式使用
Validate 方法。)在你自己的
代码,您应该检查
Page.IsValid 属性返回 true
在处理输入之前。虽然
支持脚本的浏览器可能会阻止
发生在的回发
客户端如果验证检查有
失败了,你还应该检查
之前服务器代码中的Page.IsValid
处理经过验证的数据。

那么,是否正在测试页面加载中的 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

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

So, are in testing for Page.IsValid in your Page Load?

百思不得你姐 2024-11-08 19:52:15

尝试完全删除 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 (except RequiredField) 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 property ValidateEmptyText="true" solves the issue. Sad that I had to start this giant question just to find that, but now we know! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文