How do you determine which validator failed?
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();
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 IfEnd If
In the markup, you can...
- You can put "text" on your validator (like anasterisk...)
- Or use a validation_summary control (which requires an error message on your validator)...
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 IfNext