共用方式為


ValidatorCollection.Contains(IValidator) 方法

定義

判斷指定的驗證伺服器控制項是否包含在頁面的 ValidatorCollection 集合中。

public:
 bool Contains(System::Web::UI::IValidator ^ validator);
public bool Contains (System.Web.UI.IValidator validator);
member this.Contains : System.Web.UI.IValidator -> bool
Public Function Contains (validator As IValidator) As Boolean

參數

validator
IValidator

要檢查的驗證伺服器控制項。

傳回

如果驗證伺服器控制項在集合中,則為 true,否則為 false

範例

下列程式碼範例會示範如何使用 Contains 方法。

Controls.Add(myForm);
myForm.Controls.Add(myLabel1);
myForm.Controls.Add(myTextBox);
myForm.Controls.Add(myButton);
this.Validators.Add(myRequiredFieldValidator);
myForm.Controls.Add(myLabel);

// Remove the RequiredFieldValidator.
if(Validators.Contains(myRequiredFieldValidator))
{
   this.Validators.Remove(myRequiredFieldValidator);
   Response.Write("RequiredFieldValidator is removed from ValidatorCollection<br>");
   Response.Write("ValidatorCollection count after removing the Validator: "+Validators.Count+"<br>");
}
else
{
   Response.Write("ValidatorCollection does not contain RequiredFieldValidator");
}
 Controls.Add(myForm)
 myForm.Controls.Add(myLabel1)
 myForm.Controls.Add(myTextBox)
 myForm.Controls.Add(myButton)
 Me.Validators.Add(myRequiredFieldValidator)
 myForm.Controls.Add(myLabel)

' Remove the RequiredFieldValidator.
 If Validators.Contains(myRequiredFieldValidator) Then
    Me.Validators.Remove(myRequiredFieldValidator)
    Response.Write("RequiredFieldValidator is removed from ValidatorCollection<br>")
    Response.Write("ValidatorCollection count after removing the Validator: " + Validators.Count.ToString() + "<br>")
 Else
    Response.Write("ValidatorCollection does not contain RequiredFieldValidator")
 End If

適用於

另請參閱