2 // System.Web.UI.WebControls.BaseValidator.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (C) Gaurav Vaish (2002)
11 // (C) 2003 Andreas Nahr
15 using System.ComponentModel;
16 using System.ComponentModel.Design;
21 namespace System.Web.UI.WebControls
23 [DefaultProperty("ErrorMessage")]
24 [Designer ("System.Web.UI.Design.WebControls.BaseValidatorDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
25 public abstract class BaseValidator: Label, IValidator
28 private bool isPreRenderCalled;
29 private bool isPropertiesChecked;
30 private bool propertiesValid;
31 private bool renderUplevel;
33 protected BaseValidator() : base()
36 ForeColor = Color.Red;
37 propertiesValid = true;
38 isPropertiesChecked = false;
39 renderUplevel = false;
42 [DefaultValue (""), WebCategory ("Behavior")]
43 [TypeConverter (typeof (ValidatedControlConverter))]
44 [WebSysDescription ("The ID of the control to validate.")]
45 public string ControlToValidate
49 object o = ViewState["ControlToValidate"];
58 ViewState["ControlToValidate"] = value;
62 [DefaultValue (typeof (ValidatorDisplay), "Static"), Bindable (true), WebCategory ("Appearance")]
63 [WebSysDescription ("Determines how the validator is displayed.")]
64 public ValidatorDisplay Display
68 object o = ViewState["Display"];
71 return (ValidatorDisplay)o;
73 return ValidatorDisplay.Static;
77 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
79 throw new ArgumentException();
81 ViewState["ValidatorDisplay"] = value;
85 [DefaultValue (true), WebCategory ("Behavior")]
86 [WebSysDescription ("Determines if client script is activated on uplevel browsers.")]
87 public bool EnableClientScript
91 object o = ViewState["EnableClientScript"];
100 ViewState["EnableClientScript"] = value;
104 public override bool Enabled
114 base.Enabled = value;
118 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
119 [WebSysDescription ("An error message that is displayed if the control validates to false.")]
120 public string ErrorMessage
124 object o = ViewState["ErrorMessage"];
133 ViewState["ErrorMessage"] = value;
137 [DefaultValue (null)]
138 public override Color ForeColor
142 return base.ForeColor;
146 base.ForeColor = value;
150 [DefaultValue (true), Browsable (false), WebCategory ("Misc")]
151 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
152 [WebSysDescription ("Indicates if the control validated to false.")]
155 get { return isValid; }
156 set { isValid = value; }
159 public static PropertyDescriptor GetValidationProperty(object component)
161 System.ComponentModel.AttributeCollection coll = TypeDescriptor.GetAttributes (component);
162 Type type = typeof (ValidationPropertyAttribute);
163 ValidationPropertyAttribute attrib = (ValidationPropertyAttribute) coll [type];
164 if (attrib != null && attrib.Name != null)
165 return (TypeDescriptor.GetProperties (component)) [attrib.Name];
169 public void Validate()
171 if(!Visible || (Visible && !Enabled))
175 Control ctrl = Parent;
185 isPropertiesChecked = false;
191 IsValid = EvaluateIsValid();
194 protected bool PropertiesValid
198 if(!isPropertiesChecked)
200 propertiesValid = ControlPropertiesValid();
201 isPropertiesChecked = true;
203 return propertiesValid;
207 protected bool RenderUplevel
211 return renderUplevel;
215 protected override void AddAttributesToRender(HtmlTextWriter writer)
217 bool enabled = base.Enabled;
221 base.AddAttributesToRender(writer);
226 writer.AddAttribute("id", ClientID);
228 if(ControlToValidate.Length > 0)
230 writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
232 if(ErrorMessage.Length > 0)
234 writer.AddAttribute("errormessage", ErrorMessage, true);
236 if(Display == ValidatorDisplay.Static)
238 writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
239 //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
243 writer.AddAttribute("isvalid", "False");
246 ControlStyle.AddAttributesToRender (writer, this);
249 writer.AddAttribute("enabled", "False");
259 protected void CheckControlValidationProperty(string name, string propertyName)
261 Control ctrl = NamingContainer.FindControl(name);
264 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
265 name, propertyName/*, ID*/));
267 PropertyDescriptor pd = GetValidationProperty(ctrl);
270 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
271 name, propertyName/*, ID*/));
275 protected virtual bool ControlPropertiesValid()
277 if(ControlToValidate.Length == 0)
279 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
281 CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
285 protected virtual bool DetermineRenderUplevel()
288 if(page == null || page.Request == null)
293 if(EnableClientScript)
295 if(page.Request.Browser.MSDomVersion.Major > 4)
297 return page.Request.Browser.EcmaScriptVersion.CompareTo(new Version(1,2)) >= 0;
304 protected string GetControlRenderID(string name)
306 Control ctrl = FindControl(name);
309 return ctrl.ClientID;
314 protected string GetControlValidationValue(string name)
316 Control ctrl = NamingContainer.FindControl(name);
319 PropertyDescriptor pd = GetValidationProperty(ctrl);
322 object item = pd.GetValue (ctrl);
323 if (item is ListItem)
324 return ((ListItem) item).Value;
329 return item.ToString ();
335 protected override void OnInit(EventArgs e)
338 Page.Validators.Add(this);
341 protected override void OnPreRender(EventArgs e)
344 isPreRenderCalled = true;
345 isPropertiesChecked = false;
346 renderUplevel = DetermineRenderUplevel();
349 RegisterValidatorCommonScript();
353 protected override void OnUnload(EventArgs e)
357 Page.Validators.Remove(this);
362 [MonoTODO("Damn_This_Is_Really_Frustrating___by_Gaurav")]
363 protected void RegisterValidatorCommonScript()
365 if(Page.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
368 string jsDirectory = System.Web.UI.Utils.GetScriptLocation(Context);
369 string jsFile = jsDirectory + "/WebUIValidation.js";
370 //TODO: Ok, now add the <script language="javascript"> etc
371 //FIXME: Should I check for 'Explorer'? MS-Net seems to do it!
372 throw new NotImplementedException();
375 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
376 protected virtual void RegisterValidatorDeclaration()
378 //FIXME: How to make is more abstract?
379 //Browser Info... but future browsers???
380 //I'm confused! This will make it work, at least on IE
381 string val = "document.all[\"" + ClientID;
383 Page.RegisterArrayDeclaration("Page_Validators", val);
386 [MonoTODO("Render_ing_always_left")]
387 protected override void Render (HtmlTextWriter writer)
394 if (isPreRenderCalled) {
397 isPropertiesChecked = true;
398 propertiesValid = true;
399 renderUplevel = false;
403 if (PropertiesValid) {
405 Page.VerifyRenderingInServerForm (this);
407 ValidatorDisplay dis = Display;
409 //FIXME: as of now, don't do client-side validation
410 throw new NotImplementedException();
414 RenderBeginTag (writer);
415 if (Text.Trim ().Length > 0 || HasControls ())
416 RenderContents (writer);
418 writer.Write (ErrorMessage);
419 RenderEndTag (writer);
423 writer.Write (" ");
427 protected abstract bool EvaluateIsValid();