one,
The Page.IsValid property, which gets a value that indicates whether the page validation was successful.
true if page validation was successful ; otherwise, false .
For this property to return true , all validation server controls in the current validation group must validate successfully. This property can be selected only after the Page.Validate method has been called , or the CausesValidation property has been set to true in the OnServerClick event handler of the ASP.NET server control that started form processing . These server controls include the Button , HtmlButton , HtmlInputButton , HtmlInputImage , ImageButton , and LinkButton classes.
If the validation group is forced to be validated using the Validate method, then all validation controls in the specified validation group must also validate successfully.
two,
Usually, we can check form input items by setting various validation controls on ASPX, but this function is overwritten after JavaScript is turned off on the client side.
In order to prevent some bad guys from doing damage, ASP.NET will execute Page.Validate() after the Page_Load event to start the server-side validation code of the validation control, and the validation result will be put into Page.IsValid, so that we can use it in the subsequent code. You can use Page.IsValid to determine whether all data is legal.
And if we want to know whether there is illegal data in the Page_Load event, we need to call Page.Validate() in the Page_Load event first to check.
The button's click event processing code is fine (of course, if the ValidationGroup is not set)
//
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
Page.Validate();
if (Page.IsValid == true)
{//Validation successful
DoSomeThing();
}
else
{//Validation failure
return;
}
}
}
//After Page.Validate has been executed
protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid == true)
{//Validation successful
DoSomeThing();
}
else
{//Verification failed
return;
}
}
Explanation: Verify
Whether all the validation controls in the page pass... But the validation controls are equivalent to js validation. If they do not pass, they will not pass the client-side validation. Obviously, they will not go to the background to execute this page.isvalid. You can set the validation controls of
Causescriptvalidate is false to ignore client-side validation and perform background validation. Isn't the meaning of this validation control lost... Later, I found that in the browser, you can modify the corresponding value after js is executed, which will cause dirty data. ,This
This is the advantage of using page.isvalid in the background: in order to prevent some (strongman) (updates), adding page.isvalid here is equivalent to double-layer insurance
备注
若要使该属性返回 true,Page.Validators 属性中的所有验证服务器控件必须都验证成功。只有在已调用 Page.Validate 方法,或已在开始窗体处理的 ASP.NET 服务器控件的 OnServerClick 处理程序中将 CausesValidation 属性设置为 true 后才可以选中该属性。这些服务器控件包括 Button、HtmlButton、HtmlInputButton、HtmlInputImage、ImageButton 以及 LinkButton 类。
示例
[Visual Basic, C#, JScript] 下面的示例说明使用 IsValid 属性设置条件语句。如果该属性返回 true,则 lblOutput 控件的 Text 属性被设置为“Page is valid!”。否则,它被设置为“Some of the required fields are empty”。
[Visual Basic]
Sub ValidateBtn_Click(sender As Object, e As EventArgs)
If (Page.IsValid) Then
lblOutput.Text = "Page is Valid!"
Else
lblOutput.Text = "Some of the required fields are empty"
End If
End Sub
不能太依赖客户端验证,服务器端也要再验证
customvalidator控件在焦点移开验证控件时不会进行验证,通过比如按钮的事件来检测是否page.isvalid。