c# - ASP.NET Validator 的可靠性如何?

标签 c# javascript .net validation code-behind

我决定使用 ASP.NET Validator 来验证我的 WebForm 的大量输入。它在客户端 端运行良好。我的意思是,我完全可以在没有“回发页面”等的情况下验证输入。

但是当我尝试在code behind 上使用Validator 时,它给了我Page.Isvalid 属性,我对其可靠性感到困惑。以下是我自己无法回答的问题:

  1. Validator 是否足够可靠,可以在客户端使用? (除了禁用javascript,是否可以被操纵?)
  2. 这个验证器如何到达服务器端的有效性信息? (某处是否有生成的 C# 验证器代码,或者它是否直接从客户端获取信息?)

这是我编写的用于在服务器端验证我的输入的代码:

foreach (IValidator iValidator in Page.Validators)
            {
                if (!iValidator.IsValid) { return false; }
            }

但是,它是否独立于 .aspx 和 .js?我可以在服务器端依赖它吗?

最佳答案

您也应该始终在服务器端进行验证,这主要是通过调用 Page.Validate 自动完成的。

来自 MSDN :

This method is invoked when a user clicks any ASP.NET server control that has the CausesValidation property set to true, which is the default. These include the Button, ImageButton, and LinkButton Web server controls, the HtmlInputButton, HtmlInputImage, and HtmlButton HTML server controls, and controls that can automatically post back to the server such as the TextBox, CheckBox, ListControl, and BulletedList controls.

如果你想强制验证不同的ValidationGroup,你可以手动调用它:

Page.Validate("MyValidationGroup");
If(Page.IsValid)
{
    // ...
}

请注意,只有在调用了 Page.Validate 方法或将 CausesValidation 属性设置为 true 后,才应检查 Page.IsValid在启动表单处理的 ASP.NET 服务器控件的 OnServerClick 事件处理程序中。

关于c# - ASP.NET Validator 的可靠性如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14834758/

相关文章:

将颜色着色一定百分比的 C# 算法

c# - MySQL实时更新异常

javascript - 在 AngularJS 中用公式调用 Controller

c# - GetMethod 重载返回 null

.net - EPPlus 无效地址格式错误

c# - 远程服务器返回错误 : (500) Internal Server Error. GetRequest() C#

c# - SQL 行更新在 C# 中不起作用

javascript - Angular(JavaScript)确认密码验证消息?

javascript - React、Apollo 2、GraphQL、身份验证。如何在登录后重新渲染组件

Int32 文字到 float 的 C# 性能损失