DISABLING THE CONTROLTOVALIDATE PROPERTY
My DualValidator uses the values from two controls that are identified via the FirstControlToValidate property and the SecondControlToValidate property. Since I have no need of the ControlToValidate property, I have hidden it from the property browser of Visual Studio.NET and have also raised an exception if a user tries to set the property’s value in the codebehind file.
/// Disable ControlToValidate.
[Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public new string ControlToValidate { get { return ""; } set { throw new NotSupportedException("Assign ControlToValidate on FirstControlToValidate and SecondControlToValidate instead."); }
} /// Since we are not using ControlToValidate, this should always return true.
protected override bool ControlPropertiesValid()
{ return true;
} |
|
EvaluateIsValid THE HEART OF SERVER-SIDE VALIDATION
Normally, the EvaluateIsValid method is used to determine if the value in the ControlToValidate property is valid. However, in my case, I disabled the ControlToValidate property and created three new properties for validating. This validator validates the values in two controls if and only if the value in the third control had a value. The property called PrimaryControl is used to determine whether we will do any validation on the FirstControlToValidate and SecondControlToValidate properties. In order to keep this method clean and easily readable, I created a new method called ValidateDualControls that actually performs the validation. It receives three parameters. These parameters contain the value for the three properties listed above, the PrimaryControl, FirstControlToValidate and SecondControlToValidate properties that represent the controls that are involved in the validation routine.
protected override bool EvaluateIsValid() {
return
ValidateDualControls(this.PrimaryControl,this.FirstControlToValidate, this.SecondControlToValidate);
} protected bool ValidateDualControls(string primarycontrol, string
control1, string control2) { string FirstControl_Value = ""; string SecondControl_Value = ""; |