如果我总是叫Page.IsValid?我总是、Page、IsValid

2023-09-02 10:46:50 作者:放肆的沉沦

我知道永远不要相信用户的输入,因为不良的输入可能会以某种方式损害应用程序的完整性,无论是有意或无意,但是,有没有调用Page.IsValid即使没有验证控件的页面(再次,我知道它不好的做法,通过省略验证被信任用户输入)的情况下?是否Page.IsValid执行任何其他类型的验证?我看了看MSDN和文档似乎表明Page.IsValid如果在页面上验证控件是唯一有效的,或者Page.Validate方法被调用。我的一个朋友建议我每次总是检查Page.IsValid在按钮单击处理程序,即使没有验证控件或显式Page.Validate电话。

I know to never trust user input, since undesirable input could be compromise the application's integrity in some way, be it accidental or intentional; however, is there a case for calling Page.IsValid even when no validation controls are on the page (again, I know its bad practice to be trusting user input by omitting validation)? Does Page.IsValid perform any other kinds of validation? I looked at MSDN, and the docs seem to suggest that Page.IsValid is only effective if there are validation controls on the page, or the Page.Validate method has been called. A friend of mine suggested that I always check Page.IsValid in the button click handlers every time even if there are no validation controls or explicit Page.Validate calls.

推荐答案

我会是第一个告诉你的所有输入是邪恶的,直到证明并非如此。的但是,在这种情况下,我觉得你的朋友是错误的,因为他/她的逻辑,我们也许可以想出应该检查或设置,即使违约也还行百等性能。

I would be the first to tell you that "All input is evil until proven otherwise." However, in this case, I think your friend is mistaken because by his/her logic we could probably come up with a hundred other properties that should be checked or set, even though the defaults are okay.

检查 Page.IsValid 才有意义,如果你有一个的CausesValidation方案 - 一个按钮,提交表单都有的CausesValidation 属性设置为True。这将自动调用 Page.Validate 并属于同一所有验证控件 ValidationGroup 将被检查的有效性。

Checking Page.IsValid only makes sense if you have a "CausesValidation" scenario - a button that submitted the form has its CausesValidation property set to True. This would automatically call Page.Validate and all Validation controls belonging to the same ValidationGroup would be checked for validity.

编辑:

刚才检查它使用反射和功能将始终返回true,如果该页面没有任何验证程序(ValidatorCollection为空)。

Just checked it using Reflector and the function will always return True if the Page does not have any Validators(ValidatorCollection is null).