views:

232

answers:

3

Quick question:

I have multiple RequireFieldValidators on my aspx page.

On the backend (C#) I want to be able to tell which control specifically wasn't valid so I can apply a style to that control. I use the Page.IsValid method to see if the overall page passed validation but I need to know specifically which one control failed.

Thanks.

+1  A: 

All Validators are added to the ValidatorCollection of the Page (property Page.Validators).

You can loop through this collection to validate each control manually.

Call method IValidator.Validate();

bob
A: 

From memory, after calling Page.Validate() you can then check the individual validators to see which are valid using IsValid on the validator.

Arry
+2  A: 

As others have mentioned, you need to loop the validator collection of the page and check their states. MSDN has examples here.

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

Nikki9696