asp:RequiredFieldValidator 不验证隐藏字段

asp:RequiredFieldValidator does not validate hidden fields(asp:RequiredFieldValidator 不验证隐藏字段)

本文介绍了asp:RequiredFieldValidator 不验证隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 ASP.NET 验证器不验证隐藏字段.我收到这样的消息:

It seems that ASP.NET validators do not validate hidden fields. I get messages like this:

无法验证由hiddenFieldValidator"的 ControlToValidate 属性引用的控件hiddenField".

Control 'hiddenField' referenced by the ControlToValidate property of 'hiddenFieldValidator' cannot be validated.

我的页面中有一个 <asp:HiddenField>,它用一些值填充客户端.我需要它在服务器上出现一次,所以我向它添加了一个 RequiredFieldValidator.

I have an <asp:HiddenField> in my page which gets filled client side with some value. I need this to be present once on the server so I added a RequiredFieldValidator to it.

它不起作用!

据我所知,作为一种解决方法,我可以:

As I see it, as a workaround, I can:

1. 使用自定义验证器,不要将其绑定到隐藏字段,只需在 OnServerValidate 上调用一个方法;

1. use a custom validator and not tie it to the hidden field, just call a method on OnServerValidate;

2. 与 CSS 样式 display:none 一起使用,它应该可以工作.

2. Use a <asp:TextBox> with a CSS style display:none and it should work.

但我想确保我没有在这里遗漏任何东西.是否可以以与其他文本字段相同的方式验证隐藏字段?或许还有第三种更优雅的选择?

But I want to make sure I am not missing something here. Is it possible or not to validate a hidden field in the same way as the other text fields? O maybe a third, more elegant option?

TIA!

推荐答案

正如您收到的异常消息所说,HiddenField 控件似乎无法直接成为标准验证控件的目标.我会使用 CustomValidator 解决方法.

Just as the exception message you're getting says, it seems HiddenField controls can't be targeted by the standard validation controls directly. I would go with the CustomValidator workaround.

这篇关于asp:RequiredFieldValidator 不验证隐藏字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:asp:RequiredFieldValidator 不验证隐藏字段

基础教程推荐