Page.IsValid Page.IsValid Page.IsValid Page.IsValid Property


取得值,指出網頁驗證是否成功。Gets a value indicating whether page validation succeeded.

 property bool IsValid { bool get(); };
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的屬性會設定為「網頁有效!」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)
    if (Page.IsValid == true)
        lblOutput.Text = "Page is Valid!";
        lblOutput.Text = "Some required fields are empty.";
Sub ValidateBtn_Click(sender As Object, e As EventArgs)
    If (Page.IsValid) Then
        lblOutput.Text = "Page is Valid!"
        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在呼叫方法之後, 才應該檢查這個屬性, 或在起始表單處理的 ASP.NET OnServerClick伺服器控制項的事件處理常式中, CausesValidation將屬性設定為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. 這些伺服器控制項包括ButtonHtmlButtonHtmlInputButtonHtmlInputImageImageButton和類別。LinkButtonThese 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.