Programmatically adding a custom validator?

Posted on 2006-03-31
Last Modified: 2012-05-05
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)

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.
Question by:g-spot
    LVL 15

    Accepted Solution

    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:

    Author Comment

    Brilliant. Thanks.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now