Advertisement
priore

A custom validator which highlights with the colored border

Feb 13th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement