views:

27

answers:

1

I am attempting to inherit an ASP.NET RegularExpressionValidator and add some functionality to it. I inherited the control and added my custom properties. However, I would also like the client-side functionality to call a different JavaScript function.

In order to do that, I will need to supress what is happening in the AddAttributesToRender method because the name of the function is hard-coded there. Here is what comes up in Reflector for the AddAttributesToRender function in RegularExpressionValidator:

Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
    MyBase.AddAttributesToRender(writer)
    If MyBase.RenderUplevel Then
        Dim clientID As String = Me.ClientID
        Dim writer2 As HtmlTextWriter = IIf(MyBase.EnableLegacyRendering, writer, Nothing)
        MyBase.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RegularExpressionValidatorEvaluateIsValid", False)
        If (Me.ValidationExpression.Length > 0) Then
            MyBase.AddExpandoAttribute(writer2, clientID, "validationexpression", Me.ValidationExpression)
        End If
    End If
End Sub

Ok, so at first glance it looks like the solution is easy: copy and paste all of the functionality to my new class and change the function name. However, note that the first line also calls AddAttributesToRender method in the superclass. This would be the grandparent superclass of my custom class. Here is the hierarchy:

BaseValidator > RegularExpressionValidator > MyCustomRegularExpressionValidator

What I am wondering is...is there a way to do something like MyBase.MyBase.AddAttributesToRender(writer) so I can skip the AddAttributesToRender call to the immediate superclass, but call the one in BaseValidator? I tried this syntax as well as casting MyBase to the type BaseValidator, but neither worked.

Or is the only solution to grab the code from every base class and put it in my custom one? Note that the BaseValidator passes the call on to its parent and so on for 3 more levels.

Seems like a lot to do when my only task is to supress one line from my immediate superclass and replace it in my custom class. This is the line:

MyBase.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RegularExpressionValidatorEvaluateIsValid", False)

Note: I tried to add the expando property before the parent class does to see if the code would check for its existence and skip it, but that just causes an exception.

A: 

I would use the StackTrace to see in the Parent if the method was being called by GrandParent (via MyBase.[]), and if that's the case, then route the call to Child (with another MyBase.[] call.

Shawn de Wet
I am not sure what you mean by this. Reflector is a much better means to determine if the grandparent is being called than using a stack trace, and I have already confirmed it.
NightOwl888
Yup, I mean use the StackTrace as follows:using System.Diagnostics;// get call stackStackTrace stackTrace = new StackTrace();// get calling method nameIf (stackTrace.GetFrame(1).GetMethod().Name) == [whatever] do something;else do something else;
Shawn de Wet