2 * Namespace: System.Web.UI.WebControls
\r
3 * Class: BaseValidator
\r
5 * Author: Gaurav Vaish
\r
6 * Maintainer: gvaish@iitk.ac.in
\r
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
\r
8 * Implementation: yes
\r
11 * (C) Gaurav Vaish (2001)
\r
15 using System.ComponentModel;
\r
17 using System.Web.UI;
\r
18 using System.Drawing;
\r
20 namespace System.Web.UI.WebControls
\r
22 [DefaultProperty("ErrorMessage")]
\r
23 //TODO: [Designer("??")]
\r
24 public abstract class BaseValidator: Label, IValidator
\r
26 private bool isValid;
\r
27 private bool isPreRenderCalled;
\r
28 private bool isPropertiesChecked;
\r
29 private bool propertiesValid;
\r
30 private bool renderUplevel;
\r
32 protected BaseValidator() : base()
\r
35 ForeColor = Color.Red;
\r
38 public string ControlToValidate
\r
42 object o = ViewState["ControlToValidate"];
\r
47 return String.Empty;
\r
51 ViewState["ControlToValidate"] = value;
\r
55 public ValidatorDisplay Display
\r
59 object o = ViewState["Display"];
\r
62 return (ValidatorDisplay)o;
\r
64 return ValidatorDisplay.Static;
\r
68 if(!Enum.IsDefined(typeof(ValidatorDisplay), value))
\r
70 throw new ArgumentException();
\r
72 ViewState["ValidatorDisplay"] = value;
\r
76 public bool EnableClientScript
\r
80 object o = ViewState["EnableClientScript"];
\r
89 ViewState["EnableClientScript"] = value;
\r
93 public override bool Enabled
\r
105 public string ErrorMessage
\r
109 object o = ViewState["ErrorMessage"];
\r
114 return String.Empty;
\r
118 ViewState["ErrorMessage"] = value;
\r
122 public override Color ForeColor
\r
134 public bool IsValid
\r
138 object o = ViewState["IsValid"];
\r
147 ViewState["IsValid"] = value;
\r
151 public static PropertyDescriptor GetValidationProperty(object component)
\r
153 ValidationPropertyAttribute attrib = (ValidationPropertyAttribute)((TypeDescriptor.GetAttributes(component))[typeof(ValidationPropertyAttribute)]);
\r
154 if(attrib != null && attrib.Name != null)
\r
156 return (TypeDescriptor.GetProperties(component, null))[attrib.Name];
\r
161 public void Validate()
\r
163 if(!Visible || (Visible && !Enabled))
\r
167 Control ctrl = Parent;
\r
168 while(ctrl != null)
\r
175 ctrl = ctrl.Parent;
\r
177 isPropertiesChecked = false;
\r
178 if(!PropertiesValid)
\r
183 IsValid = EvaluateIsValid();
\r
186 protected bool PropertiesValid
\r
190 if(!isPropertiesChecked)
\r
192 propertiesValid = ControlPropertiesValid();
\r
193 isPropertiesChecked = true;
\r
195 return propertiesValid;
\r
199 protected bool RenderUplevel
\r
203 return renderUplevel;
\r
207 protected override void AddAttributesToRender(HtmlTextWriter writer)
\r
209 bool enabled = Enabled;
\r
214 AddAttributesToRender(writer);
\r
219 writer.AddAttribute("id", ClientID);
\r
221 if(ControlToValidate.Length > 0)
\r
223 writer.AddAttribute("controltovalidate", GetControlRenderID(ControlToValidate));
\r
225 if(ErrorMessage.Length > 0)
\r
227 writer.AddAttribute("errormessage", ErrorMessage, true);
\r
229 if(Display == ValidatorDisplay.Static)
\r
231 writer.AddAttribute("display", Enum.Format(typeof(ValidatorDisplay), Display, "G").Replace('_','-'));
\r
232 //writer.AddAttribute("display", PropertyConverter.EnumToString(typeof(ValidatorDisplay), Display));
\r
236 writer.AddAttribute("isvalid", "False");
\r
240 writer.AddAttribute("enabled", "False");
\r
250 protected void CheckControlValidationProperty(string name, string propertyName)
\r
252 Control ctrl = NamingContainer.FindControl(name);
\r
255 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_not_found",
\r
256 name, propertyName/*, ID*/));
\r
258 PropertyDescriptor pd = GetValidationProperty(ctrl);
\r
261 throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_control_type",
\r
262 name, propertyName/*, ID*/));
\r
266 protected virtual bool ControlPropertiesValid()
\r
268 if(ControlToValidate.Length == 0)
\r
270 throw new HttpException(HttpRuntime.FormatResourceString("Validator_control_blank", ID));
\r
272 CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
\r
277 protected virtual bool DetermineRenderUplevel()
\r
280 if(page == null || page.Request == null)
\r
284 if(EnableClientScript)
\r
286 throw new NotImplementedException();
\r
287 //TODO: I need to get the (Browser->Dom_version_major >= 4 &&
\r
288 // Brower->Ecma_script_version >= 1.2)
\r
293 protected string GetControlRenderID(string name)
\r
295 Control ctrl = FindControl(name);
\r
298 return ctrl.ClientID;
\r
300 return String.Empty;
\r
303 protected string GetControlValidationValue(string name)
\r
305 Control ctrl = NamingContainer.FindControl(name);
\r
308 PropertyDescriptor pd = GetValidationProperty(ctrl);
\r
311 object item = pd.GetValue(ctrl);
\r
312 if(item is ListItem)
\r
314 return ((ListItem)item).Value;
\r
316 return item.ToString();
\r
322 protected override void OnInit(EventArgs e)
\r
325 Page.Validators.Add(this);
\r
328 protected override void OnPreRender(EventArgs e)
\r
331 isPreRenderCalled = true;
\r
332 isPropertiesChecked = false;
\r
333 renderUplevel = DetermineRenderUplevel();
\r
336 RegisterValidatorCommonScript();
\r
340 protected override void OnUnload(EventArgs e)
\r
344 Page.Validators.Remove(this);
\r
349 [MonoTODO("What_do_I_have_to_do")]
\r
350 protected void RegisterValidatorCommonScript()
\r
352 throw new NotImplementedException();
\r
355 [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
\r
356 protected virtual void RegisterValidatorDeclaration()
\r
358 throw new NotImplementedException();
\r
359 //TODO: Since I have to access document.<ClientID> and register
\r
360 // as page validator. Now this is Browser dependent :((
\r
363 [MonoTODO("Render_ing_always_left")]
\r
364 protected override void Render(HtmlTextWriter writer)
\r
367 if(isPreRenderCalled)
\r
369 valid = (Enabled && IsValid);
\r
372 isPropertiesChecked = true;
\r
373 propertiesValid = true;
\r
374 renderUplevel = false;
\r
377 if(PropertiesValid)
\r
381 Page.VerifyRenderingInServerForm(this);
\r
383 ValidatorDisplay dis = Display;
\r
386 throw new NotImplementedException();
\r
388 throw new NotImplementedException();
\r
393 protected abstract bool EvaluateIsValid();
\r