Inheriting from BaseValidator to make custom validation controls

For months now, I've been desperate for a decent excuse to make a custom ASP.NET server control, so I was secretly pleased today when a CustomValidator originally coded for use on a single page was required elsewhere.

It's surprisingly easy to do, although it took me a while to find a decent example online, which eventually came courtesy of the 4GuysFromRolla. In my particular scenario, I needed to check that the text entered into a TextBox did not equal any of a selection of "banned" strings (which was available via a static property on another class).

Having added a new custom web control, deriving from BaseValidator, my first task was therefore to ensure that the control being validated really was a TextBox - this was done by overriding the ControlPropertiesValid() method:

/// 
/// This method checks whether the control set in the 
///   ControlToValidate property is valid.
/// 
/// Boolean indicating whether we have a valid ControlToValidate
protected override bool ControlPropertiesValid()
{
   // Get the control we're trying to validate
   Control ctrl = FindControl(ControlToValidate);
   if (ctrl == null)
   {
      // If the control does not exist, throw an exception
      throw new ApplicationException(string.Format(
"StringValidator - Control {0} could not be found to validate",
ControlToValidate));
   }
   else
   {
      // Try to cast the control to a Textbox
      try
      {
         _textctrl = (TextBox)ctrl;
         return true;
      }
      catch (InvalidCastException)
      {
         // Control doesn't appear to be a TextBox - 
         // throw an exception
         throw new ApplicationException(string.Format(
         "StringValidator - Control {0} is not a TextBox",
         ControlToValidate));
      }
   }
}

That done, I just had to override the EvaluateIsValid() method, which does the actual validation:

/// 
/// Determine whether the text entered in the TextBox being
///  validated is allowed.
/// 
/// Boolean indicating whether the string 
protected override bool EvaluateIsValid()
{
   // Get dataview of invalid strings
   DataView dvInvalidStrings = SiteTerms.InvalidStrings;
   // Search dataview for the string entered in the TextBox
   //  control being validated
   dvInvalidStrings.RowFilter = string.Concat("[InvalidString] = 
  '",_textctrl.Text.ToString().Trim().Replace("'","''"),"'");
   // Return false if any rows were found, otherwise true.
   if (dvInvalidStrings.Count==0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

...and that was it. Job done, pretty much. OK, so I spent a while making a cute icon image too. But the coding was easy and straightforward - inheriting from BaseValidator meant that most of the work was already done for me. In no time at all I was able to drop this custom control onto the required pages and user controls.

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment