Programmatically adding a custom validator?

How do I programmatically add a custom validator? I thought something like this would do:

            Dim validationControl As CustomValidator
            validationControl = New CustomValidator()
            validationControl.ID = "valAllDOBValidator"
            validationControl.ControlToValidate = "dropDOBDD"
            validationControl.Text = "Error Message"
            validationControl.CssClass = "highlight"
            validationControl.Display = ValidatorDisplay.Dynamic
            validationControl.EnableClientScript = "False"
            validationControl.ServerValidate = New ServerValidateEventHandler(AddressOf ALLDOBSValidator_ServerValidate)
            plhOtherInsured.Controls.Add(validationControl)

However I'm getting the following error:

Error      29      'Public Event ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
g-spotAsked:
Who is Participating?
 
crisco96Commented:
Since this is a local variable you have to use AddHandler:

AddHandler validatoinControl.ServerValidate, AddressOf ALLDOBSValidator_ServerValidate

If it were a global varaible you could also use handles:

Protected Sub ALLDOBSValidator_ServerValidate(sender as object, args as ServerValidateEventArgs) handles validationControl.ServerValidate

...

End Sub

More info:
http://www.thescarms.com/dotnet/EventHandler.asp
0
 
g-spotAuthor Commented:
Brilliant. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.