Skip to content

Instantly share code, notes, and snippets.

@ianfnelson
Created March 28, 2014 20:36
Show Gist options
  • Save ianfnelson/9842442 to your computer and use it in GitHub Desktop.
Save ianfnelson/9842442 to your computer and use it in GitHub Desktop.
Gists for 2004 Blog Post "Inheriting From BaseValidator to Make Custom Validation Controls"
///
/// 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));
}
}
}
///
/// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment