ASP.Net 2.0 Validation Groups & IValidator Controls.

C

Craig

Ok so ASP.Net 2.0 Validation Groups are great and lovely...

But has anyone managed to sort out a solution to Validators that directly
Implement IValidator (as opposed to inheriting from BaseValidator) error
message's not appearing in the ValidationSummary.

Starting point for my control was....
http://codeproject.com/aspnet/selfvalidatingtextbox.asp,
http://www.codetools.com/aspnet/SelfValidatingTextBox2.asp

I've overridden the Validate method and GetValidators on the page to work
with validationGroups...the default page ones cast all registered validators
to BaseValidator....

But validationSummary doesn't want to play ball..Reflecting the
ValidationSummary.GetErrorMessages :

Dim collection1 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

But this isn't firing my custom Page.GetValidators call....which I find odd...

My workaround at the moment is to copy the reflected code of
ValidationSummary into a customValidationSummary control and alter that so
that it grabs the method via reflection...

'Dim collection1 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

Dim collection1 As ValidatorCollection
Dim oMethodInfo As System.Reflection.MethodInfo =
Me.Page.GetType.GetMethod("GetValidators")
collection1 = oMethodInfo.Invoke(Me.Page, New Object()
{Me.ValidationGroup})

Dim collection2 As ValidatorCollection =
Me.Page.GetValidators(Me.ValidationGroup)

This isn't that elegant so I thought I'd pitch for other
suggestions...(apart from, Create a baseValidator inherited validation
control...and replace the single self validating control with a textbox and
the new validation control.)

Cheers..

C.


Public Overrides Sub Validate(ByVal validationGroup As String)
MyBase.Validate(validationGroup)
Dim vals As ValidatorCollection = GetValidators(validationGroup)
If String.IsNullOrEmpty(validationGroup) AndAlso Me.Validators.Count
= vals.Count Then
Me.Validate()
Else
Dim i As Integer = 0
Do While (i < vals.Count)
vals.Item(i).Validate()
i += 1
Loop

End If

End Sub

Public Overloads Function GetValidators(ByVal validationgroup As String)
As ValidatorCollection
Dim coll As New ValidatorCollection

For Each obj As Object In Page.Validators
Dim val As BaseValidator = TryCast(obj, BaseValidator)

If Not val Is Nothing AndAlso
String.Compare(val.ValidationGroup, validationgroup,
StringComparison.Ordinal) = 0 Then
coll.Add(val)
Else

Dim ival As IValidator = TryCast(obj, IValidator)
If Not ival Is Nothing Then

'IValidator dsoesn't expose validation group
Dim objtype As Type = obj.GetType()

Dim prop As System.Reflection.PropertyInfo =
objtype.GetProperty("ValidationGroup", System.Reflection.BindingFlags.Public
Or System.Reflection.BindingFlags.Instance)
Dim group As String = prop.GetValue(obj,
Nothing).ToString()

If String.Compare(group, validationgroup,
StringComparison.Ordinal) = 0 Then
coll.Add(ival)
End If

End If


End If

Next

Return coll
End Function
 
C

Carl Reid

Have you found a solution to this problem? I am stuck on exactly this issue and it looks like I have got pretty much the same code.

what I don't understand is why the call to Page.GetValidators still calls the system.web.ui.page method instead of the overridden method in my custom page (which inherits from system.web.ui.page). Any more information would be invaluable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top