ASP.NET 使用不同的验证组验证所有验证器
我正在编写一个 ASP.NET 页面并尝试进行验证。我的问题是我有一个中继器,其中包含多个自定义网格控件,每个控件都有验证器和验证摘要。
起初,我没有分配任何验证组,但这最终导致每当一个网格出现错误时,每个网格都会出现验证摘要(我想是因为未设置验证摘要的验证组,这意味着它捕获了所有验证错误)。
因此,我为每个网格分配了一个单独的验证组,该验证组对于控件上的某个属性是唯一的。但我在页面底部有一个按钮(没有验证组)需要验证输入。由于没有验证组,它不会自动验证网格的验证器,因此我添加了一个调用 Page_ClientValidate()
的点击处理程序。没有骰子——验证无处不在。
好的,所以我遍历验证组,并在每个验证组有任何验证器时调用 Page_ClientValidate(validationGroup)
。当只有一个网格有验证器时工作正常,但当两个或更多网格有验证器时,它会自动隐藏除最后一个检查之外的所有验证摘要。有没有什么方法可以禁用这种行为,或者有更好的方法来完全做到这一点?
如果需要的话,我想在完成迭代验证后我可以浏览并取消隐藏其他验证摘要(尽管这可能会产生其他影响),但我还需要取消隐藏验证器显示(我正在使用图像来表示无效字段)。这似乎是一个令人讨厌且可能脆弱的解决方案。
编辑:哦,一个转折。我尝试采用我在最后提到的方法——重新显示无效的隐藏验证器/验证摘要——但 Microsoft 代码也阻止了这种情况;在 ValidatorValidate(validator, validationGroup, event)
方法(由页面中每个验证器上的 Page_ClientValidate(validationGroup)
调用)的第一行中,validator.isvalid
设置为 true
,并且仅设置为条件中验证函数的返回值,该条件仅在 validationGroup
参数与验证器的参数匹配时运行。结果是,隐藏的验证器都被标记为有效,这使得事后很难确定验证器是否有效,因为它实际上有效,还是因为微软在设计客户端验证代码时很愚蠢。
I'm writing an ASP.NET page and trying to get validation working. My problem is that I've got a repeater that contains several custom grid controls, each of which has validators and a validation summary.
At first, I didn't assign any validation groups, but this ended up making validation summaries appear for every grid whenever there was an error in one (because the validation summary's validation group was not set, I suppose, which meant it captured all validation errors).
So I assigned a separate validation group for each grid, unique with respect to a certain property on the control. But I have a button at the bottom of the page (with no validation group) that needs to validate the input. Not having a validation group, it won't automatically validate the grids' validators, so I added a click handler that calls Page_ClientValidate()
. No dice--validation appears everywhere.
Okay, so I iterate through the validation groups and call Page_ClientValidate(validationGroup)
on each if it has any validators. Works fine when only one grid has validators, but when two or more do, it automatically hides all validation summaries but the last one checked. Is there any way to disable this behavior, or a better way to do this entirely?
If I need to, I guess I can go through and unhide the other validation summaries once I've finished iteratively validating (although that might possibly have other implications), but I'd also need to unhide the validator displays (I'm using an image to denote invalid fields). It seems like such an annoying and possibly fragile solution.
EDIT: Oh, a twist. I tried doing the approach I mentioned at the end--re-showing the hidden validators/validation summaries that are invalid--but the Microsoft code prevents that, too; in the first line of the ValidatorValidate(validator, validationGroup, event)
method (called by Page_ClientValidate(validationGroup)
on each validator in the page), validator.isvalid
is set to true
, and is only set to the return value of the validation function within a conditional that only runs if the validationGroup
parameter matches the validator's. The upshot being, the hidden validators all are marked as valid, making it tough to determine after the fact whether a validator is valid because it's actually valid or because Microsoft was stupid in designing the client-side validation code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论