I am working with a page and I am getting an Page.IsValid = false and I am trying to determine which control is causing the validation problem.
4 Answers
Credit to Steven for this answer, but I had to make some changes for it to work as this.Validators.Where() had some problems.
using System.Linq;
List<IValidator> errored = this.Validators.Cast<IValidator>().Where(v => !v.IsValid).ToList();

- 1
- 1

- 488
- 4
- 8
-
3it didn't work the way @Steven said, but this answer is perfect. Only had to change the *this* for *Page*. thks – StinkyCat Aug 07 '14 at 10:08
-
Instead of using this.Validators I am using Page.Validators – JohnnyBizzle Jul 09 '15 at 16:40
-
`Page.Validators` didn't work for me.. yes, somebody out there is still using WebForms... bleh. But this answer worked fine. Thanks +1 – Piotr Kula Aug 13 '15 at 15:55
-
Building on this answer, I use the following code to identify the validator that is invalid: 'if (Page.IsValid) { // do something } else { // page is not valid List
invalides = this.Validators.Cast – CAK2 Jan 05 '18 at 05:56().Where(v => !v.IsValid).ToList(); foreach (var invalid in invalides) { Debug.WriteLine(invalid.ErrorMessage); } }'
In code (page_load), you can do this:
(per MSDN: http://msdn.microsoft.com/en-US/library/dh9ad08f%28v=VS.80%29.aspx)
If (Me.IsPostBack) Then
Me.Validate()
If (Not Me.IsValid) Then
Dim msg As String
' Loop through all validation controls to see which
' generated the error(s).
Dim oValidator As IValidator
For Each oValidator In Validators
If oValidator.IsValid = False Then
msg = msg & "<br />" & oValidator.ErrorMessage
End If
Next
Label1.Text = msg
End If
End If
In the markup, you can...
- You can put "text" on your validator (like an asterisk...)
- Or use a validation_summary control (which requires an error message on your validator)...

- 12,541
- 8
- 45
- 62
-
21
-
1@Steven -- I keep meaning to learn about that -- very cool though -- you should get credit for that. – Chains Aug 01 '11 at 16:19
-
2It is actually very easy to give me credits for that. Just click on the 'arrow up' simple on the left side of my comment ;-). – Steven Aug 01 '11 at 19:28
-
2@Steven_ That doesn't compile because a `ValidatorCollection` doesn't implement `IEnumerable
` but you could use `var v = Validators.Cast – Tim Schmelter Jun 18 '18 at 13:04().Where(v => !v.IsValid);`
The accepted answer allows you to find the validation message of the validator that failed. If you want to find the ID of the control that failed validation, it is possible to get that by casting the validator to a BaseValidator which exposes the ControlToValidate
property. For example:
For Each v As BaseValidator In Page.Validators
If Not v.IsValid Then
' You can see the control to validate name and error message here.
Debug.WriteLine(v.ControlToValidate)
Debug.WriteLine(v.ErrorMessage)
End If
Next

- 4,500
- 1
- 25
- 27
To check which Validator
is being fired, just check the HTML in Firebug and if any Validator
is not having display:none;
property or is having visibility:visible
in its properties then it is the one which is causing Page.IsValid
false
.

- 3,673
- 4
- 29
- 46

- 41
- 1