Here Is the Code sample :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ValidationHelper
{
public class ValidationBGColorAdjustor
{
public static void SetBGColor(Page page)
{
Dictionary<string, List> controls = new Dictionary<string, List>();
foreach (BaseValidator validator in page.Validators)
{
if (controls.ContainsKey(validator.ControlToValidate))
controls[validator.ControlToValidate].Add(validator.IsValid);
else
controls[validator.ControlToValidate] = new List() { validator.IsValid };
}
foreach (string s in controls.Keys)
{
bool IsValid = true;
foreach (bool b in controls[s])
IsValid &= b;
Control control = FindControlRecursive(page.Master,s);
if (control != null)
((WebControl)control).Attributes.Add("style", "background-color:"
+ (IsValid ? "''" : "#FFCACA"));
}
}
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
foreach (Control Ctl in root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
}
}
calling method
protected override void OnLoadComplete(EventArgs e)
{
ValidationBGColorAdjustor.SetBGColor(Page);
base.OnLoadComplete(e);
}
Comments
Post a Comment