X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.Web%2FSystem.Web.UI.WebControls%2FBaseValidator.cs;h=5b30e41aa85a66b2e5385f0f11066f547ad5ea3d;hb=b8893efbccd64f92c81082e42b8e480b74881927;hp=e2e2ea1dad3cecea6406ae574d92bcd615c4594b;hpb=bc380512e6e245a2ac473fe686701cea665a91f0;p=mono.git diff --git a/mcs/class/System.Web/System.Web.UI.WebControls/BaseValidator.cs b/mcs/class/System.Web/System.Web.UI.WebControls/BaseValidator.cs index e2e2ea1dad3..5b30e41aa85 100644 --- a/mcs/class/System.Web/System.Web.UI.WebControls/BaseValidator.cs +++ b/mcs/class/System.Web/System.Web.UI.WebControls/BaseValidator.cs @@ -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); @@ -267,10 +238,10 @@ namespace System.Web.UI.WebControls { protected virtual bool ControlPropertiesValid () { if (ControlToValidate.Length == 0) { - throw new HttpException("ControlToValidate property cannot be emtpy"); + throw new HttpException (String.Format ("ControlToValidate property of '{0}' cannot be blank.", ID)); } - CheckControlValidationProperty (ControlToValidate, ""); + CheckControlValidationProperty (ControlToValidate, String.Empty); return true; } @@ -279,30 +250,8 @@ namespace System.Web.UI.WebControls { { if (!EnableClientScript) return false; - - try { - if (Page == null || Page.Request == null) - return false; - } - catch { - /* this can happen with a fake Page in nunit - * tests, since Page.Context == 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); + return UplevelHelper.IsUplevel ( + System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request)); } protected abstract bool EvaluateIsValid (); @@ -328,7 +277,14 @@ namespace System.Web.UI.WebControls { return null; object o = prop.GetValue (control); - return o != null ? o.ToString () : String.Empty; + + if (o == null) + return String.Empty; + + if (o is ListItem) + return ((ListItem) o).Value; + + return o.ToString (); } public static PropertyDescriptor GetValidationProperty (object o) @@ -348,110 +304,84 @@ 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); pre_render_called = true; + + ControlPropertiesValid (); render_uplevel = DetermineRenderUplevel (); - if (render_uplevel) { + if (render_uplevel) RegisterValidatorCommonScript (); - - Page.ClientScript.RegisterOnSubmitStatement ("Mono-System.Web-ValidationOnSubmitStatement", - "if (!ValidatorOnSubmit()) return false;"); - Page.ClientScript.RegisterStartupScript ("Mono-System.Web-ValidationStartupScript", - "\n"); - } } -#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 (GetType (), "Mono-System.Web-ValidationClientScriptBlock")) - Page.ClientScript.RegisterClientScriptInclude (GetType (), "Mono-System.Web-ValidationClientScriptBlock", - Page.ClientScript.GetWebResourceUrl (GetType (), -#if NET_2_0 - "WebUIValidation_2.0.js")); -#else - "WebUIValidation.js")); -#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 (); @@ -465,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; } } @@ -492,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 = " "; - } 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; } } - }