Page.IsValid 属性

定义

获取一个值,该值指示页验证是否成功。Gets a value indicating whether page validation succeeded.

public:
 property bool IsValid { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool IsValid { get; }
member this.IsValid : bool
Public ReadOnly Property IsValid As Boolean

属性值

如果页验证成功,则为 true;否则为 falsetrue if page validation succeeded; otherwise, false.

属性

异常

IsValid 属性在发生验证之前调用。The IsValid property is called before validation has occurred.

示例

下面的代码示例演示如何使用 IsValid 属性设置条件语句。The following code example demonstrates using the IsValid property to set up a conditional statement. 如果属性返回 true,则 lblOutput 控件的 Text 属性设置为 "Page 有效!"If the property returns true, the Text property of the lblOutput control is set to "Page is valid!" 否则,它设置为 "某些必填字段为空"。Otherwise, it is set to "Some of the required fields are empty."

private void ValidateBtn_Click(Object Sender, EventArgs E)
{
    Page.Validate();
    if (Page.IsValid == true)
        lblOutput.Text = "Page is Valid!";
    else
        lblOutput.Text = "Some required fields are empty.";
}
Sub ValidateBtn_Click(sender As Object, e As EventArgs)
    Page.Validate()
    If (Page.IsValid) Then
        lblOutput.Text = "Page is Valid!"
    Else
        lblOutput.Text = "Some required fields are empty."
    End If
End Sub

注解

若要使此属性返回 true,当前验证组中的所有验证服务器控件都必须验证成功。For this property to return true, all validation server controls in the current validation group must validate successfully. 只应在调用 Page.Validate 方法后检查此属性,或将 CausesValidation 属性设置为用于启动窗体处理的 ASP.NET 服务器控件的 OnServerClick 事件处理程序中的 trueYou should check this property only after you have called the Page.Validate method, or set the CausesValidation property to true in the OnServerClick event handler for an ASP.NET server control that initiates form processing. 这些服务器控件包括 "Button"、"HtmlButton"、"HtmlInputButton"、"HtmlInputImage"、"ImageButton" 和 "LinkButton"。These server controls include the Button, HtmlButton, HtmlInputButton, HtmlInputImage, ImageButton, and LinkButton classes.

如果使用 Validate 方法强制验证验证组,则指定验证组中的所有验证控件也必须验证成功。If you force validation of a validation group using the Validate method, then all validation controls in the specified validation group must validate successfully as well.

适用于

另请参阅