.NET Framework Class Library
Page..::.Validators Property

Gets a collection of all validation controls contained on the requested page.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public ReadOnly Property Validators As ValidatorCollection
Visual Basic (Usage)
Dim instance As Page
Dim value As ValidatorCollection

value = instance.Validators
C#
[BrowsableAttribute(false)]
public ValidatorCollection Validators { get; }
Visual C++
[BrowsableAttribute(false)]
public:
property ValidatorCollection^ Validators {
    ValidatorCollection^ get ();
}
JScript
public function get Validators () : ValidatorCollection

Property Value

Type: System.Web.UI..::.ValidatorCollection
The collection of validation controls.
Remarks

You can use this property to manipulate the methods and properties of the ValidatorCollection object associated with the current Page instance. This collection contains all the validation server controls that are contained in a page.

Calling the Page..::.Validate method causes validation logic to be executed for each validation server control in the current validation group. If any of these controls do not pass, the Page..::.IsValid property returns false.

For more information on validation controls, see Validation ASP.NET Controls.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Community Content

Daniel B·
Validators collection empty for Login control

If you are automating the setup of your Validators, such as in a Page Load event, in conjunction with the Login control, you may find the Page Validators collection is unexpectedly empty.

This is because the Framework is missing a call to EnsureChildControls (a protected method of the derived Control class).

If you happen to call FindControl on the Login control or access the Login's control, Controls collection, this invokes EnsureChildControls, creating the controls collection and therefore the Page Validators collection as well.

If you only want to use the Page Validators collection and have no need to call FindControl or access the Controls collection, you could use reflection to invoke the EnsureChildControls method (Login1 is the instance of the Login control, replace with ID of your Login control):

using System.Reflection;

// ...

// Manually call EnsureChildControls (not called by Framework), so Page.Validators collection is setup

typeof(Control).InvokeMember("EnsureChildControls", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, Login1, null);

If you don't want to use reflection for some reason, you could make a dummy call to FindControl:

// Make dummy call to FindControl, so Validators collection is setup (invokes protected method EnsureChildControls(), not called by Framework)

Login1.FindControl("");

Background:

I had a routine that iterated through the page validators to set common properties and because of the above, it wasn't working. I switched to a recursive routine to iterate through the control hierarchy, but this still didn't work because I was using the HasControls method to see whether a control had any children. When I switched to the statement "if (control.Controls.Count >= 1)" it miraculously started working. This is because, as stated above, accessing the Controls property called EnsureChildControls (specifically the CompositeControl get implementation). I have now reverted to my original code...


Page view tracker