ASP.Net 2.0 Validation Groups & IValidator Controls.

Discussion in 'ASP .Net Web Controls' started by Craig, Sep 25, 2006.

  1. Craig

    Craig Guest

    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
    Craig, Sep 25, 2006
    #1
    1. Advertising

  2. Craig

    Carl Reid Guest

    Re: ASP.Net 2.0 Validation Groups

    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.
    Carl Reid, Nov 10, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    0
    Views:
    1,100
    Martin
    May 19, 2005
  2. =?Utf-8?B?QS5KLiB2YW4gZGVyIEJ1cmc=?=

    Validationgroups and IValidator problem (ASP.NET 2.0)

    =?Utf-8?B?QS5KLiB2YW4gZGVyIEJ1cmc=?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,613
    =?Utf-8?B?QS5KLiB2YW4gZGVyIEJ1cmc=?=
    Dec 9, 2005
  3. brantman

    Ivalidator and Validation Groups

    brantman, Jul 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,114
    Teemu Keiski
    Aug 1, 2006
  4. A.J. van der Burg

    Validation groups and IValidator problem (ASP.NET 2.0)

    A.J. van der Burg, Dec 9, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    172
    A.J. van der Burg
    Dec 9, 2005
  5. brantman

    IValidator and Validation Groups

    brantman, Jul 31, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    116
    brantman
    Jul 31, 2006
Loading...

Share This Page