We help IT Professionals succeed at work.

Programmatically adding a custom validator?

g-spot
g-spot asked
on
Medium Priority
1,600 Views
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)
            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.
Comment
Watch Question

Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Brilliant. Thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.