using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace ex35
{

//定义需要验证的属性
	public class RequiredFieldValidator : System.Web.UI.WebControls.RequiredFieldValidator
	{
		string m_sLabelControl = "";
		string m_sErrorLabelColor = "red";
		string m_sLabelColor = "black";
		bool   m_bUseErrorIndicador = false;
		string m_sErrorIndicator = "* ";
		
//定义需要验证的值来自哪一个控件的属性		
		public string LabelControl
		{
			get { return m_sLabelControl;  }
			set { m_sLabelControl = value; }
		}	

//定义错误提示信息颜色的属性	
		public string ErrorLabelColor
		{
			get { return m_sErrorLabelColor;  }
			set { m_sErrorLabelColor = value; }
		}
		
//定义提示信息颜色的属性
		public string LabelColor
		{
			get { return m_sLabelColor;  }
			set { m_sLabelColor = value; }
		}
		
//定义错误提示的属性
		public string ErrorIndicator
		{
			get { return m_sErrorIndicator;  }
			set { m_sErrorIndicator = value; }
		}

//定义是否使用错误提示的属性		
		public bool UseErrorIndicator
		{
			get { return m_bUseErrorIndicador;  }
			set { m_bUseErrorIndicador = value; }
		}
		
		protected override bool EvaluateIsValid()
		{
			bool result = base.EvaluateIsValid();
			
			Control control = FindControl(LabelControl);
			if (control != null)
			{
				Label label = control as Label;
				
				if (label != null)
				{				
					string sColorName = result ? LabelColor : ErrorLabelColor;
					label.ForeColor = Color.FromName(sColorName);
					
					if (UseErrorIndicator)
					{
						string sLabelText = label.Text;
												
						if (result)
						{
							if (sLabelText.StartsWith(ErrorIndicator))
							{
								sLabelText = sLabelText.Substring(ErrorIndicator.Length);
							}
						}
						else
						{
							if (!sLabelText.StartsWith(ErrorIndicator))
							{
								sLabelText = ErrorIndicator + sLabelText;
							}
						}
						
						label.Text = sLabelText;
					}
				}
			}
			
			return result;
		}
		
	}

//定义自定义验证的属性	
	public class CustomValidator : System.Web.UI.WebControls.CustomValidator
	{
		string m_sLabelControl = "";
		string m_sErrorLabelColor = "red";
		string m_sLabelColor = "black";
		bool   m_bUseErrorIndicador = false;
		string m_sErrorIndicator = "* ";
		
//定义需要验证的值来自哪一个控件的属性		
		public string LabelControl
		{
			get { return m_sLabelControl;  }
			set { m_sLabelControl = value; }
		}	
	
//定义错误提示信息颜色的属性	
		public string ErrorLabelColor
		{
			get { return m_sErrorLabelColor;  }
			set { m_sErrorLabelColor = value; }
		}
		
//定义提示信息颜色的属性
		public string LabelColor
		{
			get { return m_sLabelColor;  }
			set { m_sLabelColor = value; }
		}
		
//定义错误提示的属性
		public string ErrorIndicator
		{
			get { return m_sErrorIndicator;  }
			set { m_sErrorIndicator = value; }
		}

//定义是否使用错误提示的属性		
		public bool UseErrorIndicator
		{
			get { return m_bUseErrorIndicador;  }
			set { m_bUseErrorIndicador = value; }
		}
		
		protected override bool EvaluateIsValid()
		{
			bool result = base.EvaluateIsValid();
			
			Control control = FindControl(LabelControl);
			if (control != null)
			{
				Label label = control as Label;
				
				if (label != null)
				{				
					string sColorName = result ? LabelColor : ErrorLabelColor;
					label.ForeColor = Color.FromName(sColorName);
					
					if (UseErrorIndicator)
					{
						string sLabelText = label.Text;
												
						if (result)
						{
							if (sLabelText.StartsWith(ErrorIndicator))
							{
								sLabelText = sLabelText.Substring(ErrorIndicator.Length);
							}
						}
						else
						{
							if (!sLabelText.StartsWith(ErrorIndicator))
							{
								sLabelText = ErrorIndicator + sLabelText;
							}
						}
						
						label.Text = sLabelText;
					}
				}
			}
			
			return result;
		}
	
	}
	
}



