C# 我应该总是调用 Page.IsValid 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1039465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I always call Page.IsValid?
提问by mkelley33
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.
我知道永远不要相信用户输入,因为不受欢迎的输入可能会以某种方式损害应用程序的完整性,无论是偶然的还是有意的;但是,即使页面上没有验证控件,是否也有调用 Page.IsValid 的情况(同样,我知道通过省略验证来信任用户输入是一种不好的做法)?Page.IsValid 是否执行任何其他类型的验证?我查看了 MSDN,文档似乎表明 Page.IsValid 仅在页面上有验证控件或已调用 Page.Validate 方法时才有效。我的一个朋友建议我每次都检查按钮单击处理程序中的 Page.IsValid,即使没有验证控件或显式的 Page.Validate 调用也是如此。
采纳答案by Cerebrus
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.
我会第一个告诉你“所有输入都是邪恶的,除非被证明是邪恶的。”但是,在这种情况下,我认为你的朋友错了,因为根据他/她的逻辑,我们可能会想出一百种其他属性检查或设置,即使默认值没问题。
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.
Page.IsValid
仅当您有“CausesValidation”场景时,检查才有意义 - 提交表单的按钮的CausesValidation
属性设置为 True。这将自动调用,Page.Validate
并且ValidationGroup
将检查属于相同的所有验证控件的有效性。
Edit:
编辑:
Just checked it using Reflector and the function will always return True if the Page does not have any Validators(ValidatorCollection is null).
刚刚使用反射器检查过它,如果页面没有任何验证器(ValidatorCollection 为空),该函数将始终返回 True。
回答by kemiller2002
You may still want to call it, because in the future their maybe validation controls. I know this kinda falls into adding functionality based on future requirements, but it is also protecting yourself against needing to know if the page is valid and not going through all the event handlers etc. to make sure that it is there if a validator does get added. We have a rule that we always add it, so we don't have that problem of not-validating in the future.
您可能仍然想调用它,因为将来它们可能是验证控件。我知道这有点属于根据未来的需求添加功能,但它也保护自己不需要知道页面是否有效,而不是通过所有事件处理程序等来确保它在那里,如果验证器确实得到添加。我们有一个规则,我们总是添加它,所以我们将来不会遇到不验证的问题。
回答by Leyu
You can check the validity of a Page by checking the Page.IsValid property, your purpose to check the Page.IsValid might vary like
您可以通过检查 Page.IsValid 属性来检查页面的有效性,检查 Page.IsValid 的目的可能会有所不同,例如
- If you have Validators which has the EnableClientScriptproperty set to false
- If you have a server side validated Validator.
- Before performing a critical operation in a PostBack event handler body like Save, Delete, Authenticate...
- Do/display different things depending on the validity of page...
- Any thing you can think of...
- 如果您有将EnableClientScript属性设置为 false 的验证器
- 如果您有经过服务器端验证的 Validator。
- 在 PostBack 事件处理程序主体中执行关键操作之前,例如Save、Delete、Authenticate...
- 根据页面的有效性做/显示不同的事情......
- 你能想到的任何事情...
So when/where can you call Page.IsValid
那么何时/何地您可以调用 Page.IsValid
- If the page is in post back
- If the post backis caused by an input control with the CausesValidationproperty set to true.
- After a call is made to the Page.Validate, i.e after the Page.Load event.
- 如果页面在回发中
- 如果回发是由CausesValidation属性设置为true的输入控件引起的。
- 在调用Page.Validate 之后,即在 Page.Load 事件之后。
You can check Page.IsValid in the page life cycle if the place/time invoked satisfies the above criteria; otherwise the Page.IsValid will result in the System.Web.HttpExceptionbeing thrown.
如果调用的地点/时间满足上述条件,可以在页面生命周期中检查Page.IsValid;否则 Page.IsValid 将导致抛出System.Web.HttpException。
You should use Page.IsValid where it makes sense; like in the postback event handlers of input controls(with CausesValidation=true) and require the state of the page to be valid to perform their task correctly. (if you have server side validated validators or validators with client side validation switched off it becomes a MUST).
您应该在有意义的地方使用 Page.IsValid ;就像在输入控件的回发事件处理程序中一样(使用 CausesValidation=true),并且需要页面的状态有效才能正确执行它们的任务。(如果您有服务器端验证的验证器或关闭客户端验证的验证器,则它变为MUST)。
protected void btnSave_Click(object sender, EventArgs e)
{
//Note that there might be ServerSideValidation which evaluated to false.
if (!Page.IsValid)
return;
CurrentEntity.Save();
}
Finally note that Page.IsValid only checks for validation errors in the validator controls on your page, it all depends on what your validator controls do.
最后请注意, Page.IsValid 仅检查页面上验证器控件中的验证错误,这完全取决于验证器控件的作用。