Gary King

Gary King

  • NA
  • 83
  • 209.5k

How to create CustomValidator in code behind

Oct 11 2010 10:49 AM



If I have a basic page with 2 dropdownlists (myDDL1 and myDDL2).
The Items for the lists are:

myDDL1: "", "Red" and "Blue"
myDDL2: "", "Sky" and "Royal"

If the user selects "Blue", then they should also select a value from myDDL2 (if a value is not selected, then display "Please select a value" next to myDDL2.

If the user selects "Red", then I dont need any validation on myDDL2 (or maybe I should validate that the user has not selected a value from myDDL2?)

I think that what I need is a custom validator which will check if myDDL2 selected text is "Blue" and that a value has been selected on myDDL2 , but I'm not having any luck with coding.

The aspx does not have a custom validator - instead, I would like it to be created within the aspx.cs Page_Load event. Also, I would prefer this to be a server-side validation (although I would consider using client-side).

Answers (7)