SHARE
TWEET

A custom validator which highlights with the colored border

priore Feb 13th, 2016 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Security.Permissions;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9.  
  10. namespace MyNamespace
  11. {
  12.     [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
  13.     [ToolboxData("<{0}:MyCustomValidator runat=\"server\"></{0}:MyCustomValidator>")]
  14.     public class MyCustomValidator : CustomValidator
  15.     {
  16.         public MyCustomValidator()
  17.         {
  18.             this.SetFocusOnError = true;
  19.             this.ValidateEmptyText = true;
  20.         }
  21.  
  22.         protected override bool EvaluateIsValid()
  23.         {
  24.             string _target = this.ControlToValidate;
  25.             WebControl obj = this.Parent.FindControl(_target) as WebControl;
  26.  
  27.             if (obj != null)
  28.             {
  29.                 string value = this.GetControlValidationValue(_target);
  30.                 if (obj.Enabled && (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value)))
  31.                 {
  32.                     obj.BorderColor = Color.LightPink;
  33.                     return false;
  34.                 }
  35.                 else if (obj.Enabled)
  36.                     obj.BorderColor = ColorTranslator.FromHtml("#a0a0a0");
  37.                 else
  38.                     obj.BorderColor = ColorTranslator.FromHtml("#e0e0e0");
  39.             }
  40.  
  41.             return true;
  42.         }
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top