Merge pull request #2832 from razzfazz/handle_eintr
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseValidator.cs
index f5f130eadbdd4fb6780a6ac46cd16be4383edd53..5b30e41aa85a66b2e5385f0f11066f547ad5ea3d 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //     Chris Toshok (toshok@novell.com)
 //
-// (C) 2005 Novell, Inc (http://www.novell.com)
+// (C) 2005-2010 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -33,8 +33,8 @@ using System.Reflection;
 using System.Collections;
 using System.Security.Permissions;
 
-namespace System.Web.UI.WebControls {
-
+namespace System.Web.UI.WebControls
+{
        // CAS
        [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
@@ -46,6 +46,7 @@ namespace System.Web.UI.WebControls {
                bool render_uplevel;
                bool valid;
                Color forecolor;
+               bool pre_render_called = false;
 
                protected BaseValidator ()
                {
@@ -55,20 +56,12 @@ namespace System.Web.UI.WebControls {
 
                // New in NET1.1 sp1
                [Browsable(false)]
-#if ONLY_1_1
-               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
-#endif         
                [EditorBrowsable(EditorBrowsableState.Never)]
                public override string AssociatedControlID {
-                       get {
-                               return base.AssociatedControlID;
-                       }
-                       set {
-                               base.AssociatedControlID = value;
-                       }
+                       get { return base.AssociatedControlID; }
+                       set { base.AssociatedControlID = value; }
                }
 
-#if NET_2_0
                [Themeable (false)]
                [DefaultValue ("")]
                public virtual string ValidationGroup {
@@ -92,12 +85,9 @@ namespace System.Web.UI.WebControls {
                        get { return base.Text; }
                        set { base.Text = value; }
                }
-#endif
 
-#if NET_2_0
                [IDReferenceProperty (typeof (Control))]
                [Themeable (false)]
-#endif
                [TypeConverter(typeof(System.Web.UI.WebControls.ValidatedControlConverter))]
                [DefaultValue("")]
                [WebSysDescription ("")]
@@ -107,12 +97,7 @@ namespace System.Web.UI.WebControls {
                        set { ViewState ["ControlToValidate"] = value; }
                }
 
-#if NET_2_0
                [Themeable (false)]
-#endif
-#if ONLY_1_1           
-               [Bindable(true)]
-#endif         
                [DefaultValue(ValidatorDisplay.Static)]
                [WebSysDescription ("")]
                [WebCategory ("Appearance")]
@@ -121,9 +106,7 @@ namespace System.Web.UI.WebControls {
                        set { ViewState ["Display"] = (int)value; }
                }
 
-#if NET_2_0
                [Themeable (false)]
-#endif
                [DefaultValue(true)]
                [WebSysDescription ("")]
                [WebCategory ("Behavior")]
@@ -133,25 +116,15 @@ namespace System.Web.UI.WebControls {
                }
 
                public override bool Enabled {
-                       get { return ViewState.GetBool ("Enabled", true); }
-                       set { ViewState ["Enabled"] = value; }
+                       get { return ViewState.GetBool ("BaseValidatorEnabled", true); }
+                       set { ViewState ["BaseValidatorEnabled"] = value; }
                }
 
-#if NET_2_0
                [Localizable (true)]
-#endif
-#if ONLY_1_1
-               [Bindable(true)]
-#endif         
                [DefaultValue("")]
                [WebSysDescription ("")]
                [WebCategory ("Appearance")]
-#if NET_2_0
-               public
-#else
-               public virtual
-#endif
-               string ErrorMessage {
+               public string ErrorMessage {
                        get { return ViewState.GetString ("ErrorMessage", String.Empty); }
                        set { ViewState ["ErrorMessage"] = value; }
                }
@@ -167,18 +140,11 @@ namespace System.Web.UI.WebControls {
 
                [Browsable(false)]
                [DefaultValue(true)]
-#if NET_2_0
                [Themeable (false)]
-#endif
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                [WebSysDescription ("")]
                [WebCategory ("Misc")]
-#if NET_2_0
-               public
-#else
-               public virtual
-#endif
-               bool IsValid {
+               public bool IsValid {
                        get { return valid; }
                        set { valid = value; }
                }
@@ -202,9 +168,6 @@ namespace System.Web.UI.WebControls {
                        return render_uplevel;
                }
 
-               [MonoTODO()]
-               // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute 
-               // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
                protected override void AddAttributesToRender (HtmlTextWriter writer)
                {
                        /* if we're rendering uplevel, add our attributes */
@@ -214,29 +177,23 @@ namespace System.Web.UI.WebControls {
                                        writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
 
                                if (ControlToValidate != String.Empty)
-                                       writer.AddAttribute ("controltovalidate", GetControlRenderID (ControlToValidate));
+                                       RegisterExpandoAttribute (ClientID, "controltovalidate", GetControlRenderID (ControlToValidate));
 
                                if (ErrorMessage != String.Empty)
-                                       writer.AddAttribute ("errormessage", ErrorMessage);
-                               if (Text != String.Empty)
-                                       writer.AddAttribute ("text", Text);
-#if NET_2_0
+                                       RegisterExpandoAttribute (ClientID, "errormessage", ErrorMessage, true);
+
                                if (ValidationGroup != String.Empty)
-                                       writer.AddAttribute ("validationgroup", ValidationGroup);
+                                       RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup, true);
 
                                if (SetFocusOnError)
-                                       Page.ClientScript.RegisterExpandoAttribute (ClientID, "focusOnError", "t");
-#endif
-                               if (!Enabled)
-                                       writer.AddAttribute ("enabled", "false");
-
-#if NET_2_0
-                               if (Enabled && !IsValid) {
-#else
-                               if (!IsValid) {
-#endif
-                                       writer.AddAttribute ("isvalid", "false");
-                               }
+                                       RegisterExpandoAttribute (ClientID, "focusOnError", "t");
+
+                               bool enabled = IsEnabled;
+                               if (!enabled)
+                                       RegisterExpandoAttribute (ClientID, "enabled", "False");
+
+                               if (enabled && !IsValid)
+                                       RegisterExpandoAttribute (ClientID, "isvalid", "False");
                                else {
                                        if (Display == ValidatorDisplay.Static)
                                                writer.AddStyleAttribute ("visibility", "hidden");
@@ -245,12 +202,26 @@ namespace System.Web.UI.WebControls {
                                }
 
                                if (Display != ValidatorDisplay.Static)
-                                       writer.AddAttribute ("display", Display.ToString());
+                                       RegisterExpandoAttribute (ClientID, "display", Display.ToString ());
                        }
 
                        base.AddAttributesToRender (writer);
                }
 
+               internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue)
+               {
+                       RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
+               }
+
+               internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode)
+               {
+                       Page page = Page;
+                       if (page.ScriptManager != null)
+                               page.ScriptManager.RegisterExpandoAttributeExternal (this, controlId, attributeName, attributeValue, encode);
+                       else
+                               page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
+               }
+
                protected void CheckControlValidationProperty (string name, string propertyName)
                {
                        Control control = NamingContainer.FindControl (name);
@@ -270,7 +241,7 @@ namespace System.Web.UI.WebControls {
                                throw new HttpException (String.Format ("ControlToValidate property of '{0}' cannot be blank.", ID));
                        }
 
-                       CheckControlValidationProperty (ControlToValidate, "");
+                       CheckControlValidationProperty (ControlToValidate, String.Empty);
 
                        return true;
                }
@@ -279,27 +250,8 @@ namespace System.Web.UI.WebControls {
                {
                        if (!EnableClientScript)
                                return false;
-#if TARGET_J2EE
-                       if (HttpContext.Current == null)
-                               return false;
-
-                       return (
-                               /* From someplace on the web: "JavaScript 1.2
-                                * and later (also known as ECMAScript) has
-                                * built-in support for regular
-                                * expressions" */
-                               ((Page.Request.Browser.EcmaScriptVersion.Major == 1
-                                 && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
-                                || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
-
-                               /* document.getElementById, .getAttribute,
-                                * etc, are all DOM level 1.  I don't think we
-                                * use anything in level 2.. */
-                               && Page.Request.Browser.W3CDomVersion.Major >= 1);
-#else
                        return UplevelHelper.IsUplevel (
                                System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
-#endif
                }
 
                protected abstract bool EvaluateIsValid ();
@@ -352,32 +304,18 @@ namespace System.Web.UI.WebControls {
                        return null;
                }
 
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif         
-               override void OnInit (EventArgs e)
+               protected internal override void OnInit (EventArgs e)
                {
+                       Page page = Page;
                        /* according to an msdn article, this is done here */
-                       if (Page != null) {
-                               Page.Validators.Add (this);
-
-#if NET_2_0
-                               Page.GetValidators (ValidationGroup).Add (this);
-#endif
+                       if (page != null) {
+                               page.Validators.Add (this);
+                               page.GetValidators (ValidationGroup).Add (this);
                        }
                        base.OnInit (e);
                }
 
-               bool pre_render_called = false;
-
-#if NET_2_0
-               protected internal
-#else
-               protected
-#endif         
-               override void OnPreRender (EventArgs e)
+               protected internal override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
 
@@ -386,94 +324,64 @@ namespace System.Web.UI.WebControls {
                        ControlPropertiesValid ();
 
                        render_uplevel = DetermineRenderUplevel ();
-                       if (render_uplevel) {
+                       if (render_uplevel)
                                RegisterValidatorCommonScript ();
-                       }
                }
 
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif         
-               override void OnUnload (EventArgs e)
+               protected internal override void OnUnload (EventArgs e)
                {
+                       Page page = Page;
                        /* according to an msdn article, this is done here */
-                       if (Page != null) {
-                               Page.Validators.Remove (this);
-
-#if NET_2_0
-                               if (ValidationGroup != "")
-                                       Page.GetValidators (ValidationGroup).Remove (this);
-#endif
-
+                       if (page != null) {
+                               page.Validators.Remove (this);
+                               string validationGroup = ValidationGroup;
+                               if (!String.IsNullOrEmpty (validationGroup))
+                                       page.GetValidators (ValidationGroup).Remove (this);
                        }
                        base.OnUnload (e);
                }
 
                protected void RegisterValidatorCommonScript ()
                {
-                       if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock"))
-                       {
-                               Page.ClientScript.RegisterClientScriptInclude (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock",
-                                       Page.ClientScript.GetWebResourceUrl (typeof (BaseValidator), 
-#if NET_2_0
-                                               "WebUIValidation_2.0.js"));
-                               string webForm = (Page.IsMultiForm ? Page.theForm : "window");
-                               Page.ClientScript.RegisterClientScriptBlock (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock.Initialize", "WebFormValidation_Initialize(" + webForm + ");", true);
-                               Page.ClientScript.RegisterOnSubmitStatement ("Mono-System.Web-ValidationOnSubmitStatement", "if (!" + webForm + ".ValidatorOnSubmit()) return false;");
-                               Page.ClientScript.RegisterStartupScript (typeof (BaseValidator), "Mono-System.Web-ValidationStartupScript",
-@"
-" + webForm + @".Page_ValidationActive = false;
-" + webForm + @".ValidatorOnLoad();
-" + webForm + @".ValidatorOnSubmit = function () {
-       if (this.Page_ValidationActive) {
-               return this.ValidatorCommonOnSubmit();
-       }
-       return true;
-}
-", true);
-#else          
-                                               "WebUIValidation.js"));
-
-                               Page.ClientScript.RegisterOnSubmitStatement ("Mono-System.Web-ValidationOnSubmitStatement",
-                                                                            "if (!ValidatorOnSubmit()) return false;");
-                               Page.ClientScript.RegisterStartupScript ("Mono-System.Web-ValidationStartupScript",
-                                                                        "<script language=\"JavaScript\">\n" + 
-                                                                        "<!--\n" + 
-                                                                        "var Page_ValidationActive = false;\n" + 
-                                                                        "ValidatorOnLoad();\n" +
-                                                                        "\n" + 
-                                                                        "function ValidatorOnSubmit() {\n" + 
-                                                                        "        if (Page_ValidationActive) {\n" + 
-                                                                        "                if (!ValidatorCommonOnSubmit())\n" +
-                                                                        "                        return Page_ClientValidate ();\n" +
-                                                                        "        }\n" + 
-                                                                        "        return true;\n" + 
-                                                                        "}\n" + 
-                                                                        "// -->\n" + 
-                                                                        "</script>\n");
-#endif
+                       Page page = Page;
+                       if (page != null) {
+                               if (page.ScriptManager != null) {
+                                       page.ScriptManager.RegisterClientScriptResourceExternal (this, typeof (BaseValidator), "WebUIValidation_2.0.js");
+                                       page.ScriptManager.RegisterClientScriptBlockExternal (this, typeof (BaseValidator), "ValidationInitializeScript", page.ValidationInitializeScript, true);
+                                       page.ScriptManager.RegisterOnSubmitStatementExternal (this, typeof (BaseValidator), "ValidationOnSubmitStatement", page.ValidationOnSubmitStatement);
+                                       page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), "ValidationStartupScript", page.ValidationStartupScript, true);
+                               } else if (!page.ClientScript.IsClientScriptIncludeRegistered (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock")) {
+                                       page.ClientScript.RegisterClientScriptInclude (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock",
+                                                                                      page.ClientScript.GetWebResourceUrl (typeof (BaseValidator), "WebUIValidation_2.0.js"));
+                                       page.ClientScript.RegisterClientScriptBlock (typeof (BaseValidator), "Mono-System.Web-ValidationClientScriptBlock.Initialize", page.ValidationInitializeScript, true);
+                                       page.ClientScript.RegisterOnSubmitStatement (typeof (BaseValidator), "Mono-System.Web-ValidationOnSubmitStatement", page.ValidationOnSubmitStatement);
+                                       page.ClientScript.RegisterStartupScript (typeof (BaseValidator), "Mono-System.Web-ValidationStartupScript", page.ValidationStartupScript, true);
+                               }
                        }
                }
 
                protected virtual void RegisterValidatorDeclaration ()
                {
-                       Page.ClientScript.RegisterArrayDeclaration ("Page_Validators",
-                                                                   String.Format ("document.getElementById ('{0}')", ClientID));
+                       Page page = Page;
+                       if (page != null) {
+                               if (page.ScriptManager != null) {
+                                       page.ScriptManager.RegisterArrayDeclarationExternal (this, "Page_Validators", String.Concat ("document.getElementById ('", ClientID, "')"));
+                                       page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), ClientID + "DisposeScript",
+                                                                                         @"
+document.getElementById('" + ClientID + @"').dispose = function() {
+    Array.remove(Page_Validators, document.getElementById('" + ClientID + @"'));
+}
+", true);
+                               } else
+                                       page.ClientScript.RegisterArrayDeclaration ("Page_Validators", String.Concat ("document.getElementById ('", ClientID, "')"));
+                       }
                }
 
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif         
-               override void Render (HtmlTextWriter writer)
+               protected internal override void Render (HtmlTextWriter writer)
                {
-#if NET_2_0
-                       if (!Enabled && !EnableClientScript)
+                       if (!IsEnabled && !EnableClientScript)
                                return;
-#endif
+
                        if (render_uplevel) {
                                /* according to an msdn article, this is done here */
                                RegisterValidatorDeclaration ();
@@ -487,21 +395,14 @@ namespace System.Web.UI.WebControls {
                        if (!pre_render_called) {
                                render_tags = true;
                                render_text = true;
-                       }
-                       else if (render_uplevel) {
+                       } else if (render_uplevel) {
                                render_tags = true;
-#if NET_2_0
                                render_text = Display != ValidatorDisplay.None;
-#else
-                               if (Display != ValidatorDisplay.None)
-                                       render_text = !v || Display == ValidatorDisplay.Dynamic;
-#endif
-                       }
-                       else {
-                               if (Display == ValidatorDisplay.Static) {
+                       } else {
+                               if (Display != ValidatorDisplay.None) {
                                        render_tags = !v;
                                        render_text = !v;
-                                       render_nbsp = v;
+                                       render_nbsp = v && Display == ValidatorDisplay.Static;
                                }
                        }
 
@@ -514,34 +415,26 @@ namespace System.Web.UI.WebControls {
                                string text;
                                if (render_text) {
                                        text = Text;
-                                       if (text == "")
+                                       if (String.IsNullOrEmpty (text))
                                                text = ErrorMessage;
-                               } else {
+                               } else
                                        text = "&nbsp;";
-                               }
 
                                writer.Write (text);
                        }
 
 
-                       if (render_tags) {
+                       if (render_tags)
                                writer.RenderEndTag ();
-                       }
                }
 
                /* the docs say "public sealed" here */
-#if NET_2_0
-               public
-#else
-               public virtual
-#endif
-               void Validate ()
+               public void Validate ()
                {
-                       if (Enabled && Visible)
+                       if (IsEnabled && Visible)
                                IsValid = ControlPropertiesValid () && EvaluateIsValid ();
                        else
                                IsValid = true;
                }
        }
-
 }