Merge pull request #1949 from lewurm/fixtype
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
index b01661d6439d46fd541c7ec6f24e252fbec12f0d..27c3a7fb9dc870f7018c9a6956ef0871173ecb54 100644 (file)
@@ -4,7 +4,7 @@
 // Author:
 //      Dick Porter  <dick@ximian.com>
 //
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (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
@@ -31,8 +31,10 @@ using System.Collections.Specialized;
 using System.ComponentModel;
 using System.Globalization;
 using System.Security.Permissions;
+using System.Web.Util;
 
-namespace System.Web.UI.WebControls {
+namespace System.Web.UI.WebControls
+{
        // CAS
        [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
@@ -41,24 +43,16 @@ namespace System.Web.UI.WebControls {
        [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
        [DefaultEvent ("CheckedChanged")]
        [DefaultProperty ("Text")]
-#if NET_2_0
        [ControlValueProperty ("Checked", null)]
        [SupportsEventValidation]
-#endif         
-       public class CheckBox : WebControl, IPostBackDataHandler
-#if NET_2_0
-       , ICheckBoxControl
-#endif
+       public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
        {
                string render_type;
                AttributeCollection common_attrs;
-
-#if NET_2_0
                AttributeCollection inputAttributes;
                StateBag inputAttributesState;
                AttributeCollection labelAttributes;
                StateBag labelAttributesState;
-#endif
                
                public CheckBox () : base (HtmlTextWriterTag.Input)
                {
@@ -71,60 +65,36 @@ namespace System.Web.UI.WebControls {
                }
 
                [DefaultValue (false)]
-#if NET_2_0
                [Themeable (false)]
-#endif         
                [WebSysDescription ("")]
                [WebCategory ("Behavior")]
-               public virtual bool AutoPostBack 
-               {
-                       get {
-                               return (ViewState.GetBool ("AutoPostBack",
-                                                          false));
-                       }
-                       set {
-                               ViewState["AutoPostBack"] = value;
-                       }
+               public virtual bool AutoPostBack {
+                       get { return (ViewState.GetBool ("AutoPostBack", false)); }
+                       set { ViewState["AutoPostBack"] = value; }
                }
 
-#if NET_2_0
                [DefaultValue (false)]
                [Themeable (false)]
-               [MonoTODO]
                [WebSysDescription ("")]
                [WebCategory ("Behavior")]
-               public virtual bool CausesValidation 
-               {
+               public virtual bool CausesValidation {
                        get { return ViewState.GetBool ("CausesValidation", false); }
                        set { ViewState ["CausesValidation"] = value; }
                }
-#endif         
-               
 
                [DefaultValue (false)]
-#if NET_2_0
                [Bindable (true, BindingDirection.TwoWay)]
                [Themeable (false)]
-#else          
-               [Bindable (true)]
-#endif         
                [WebSysDescription ("")]
                [WebCategory ("Behavior")]
-               public virtual bool Checked 
-               {
-                       get {
-                               return (ViewState.GetBool ("Checked", false));
-                       }
-                       set {
-                               ViewState["Checked"] = value;
-                       }
+               public virtual bool Checked {
+                       get { return (ViewState.GetBool ("Checked", false)); }
+                       set { ViewState["Checked"] = value; }
                }
 
-#if NET_2_0
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
-               public AttributeCollection InputAttributes 
-               {
+               public AttributeCollection InputAttributes {
                        get {
                                if (inputAttributes == null) {
                                        if (inputAttributesState == null) {
@@ -140,8 +110,7 @@ namespace System.Web.UI.WebControls {
 
                [Browsable (false)]
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
-               public AttributeCollection LabelAttributes
-               {
+               public AttributeCollection LabelAttributes {
                        get {
                                if (labelAttributes == null) {
                                        if (labelAttributesState == null) {
@@ -154,34 +123,21 @@ namespace System.Web.UI.WebControls {
                                return labelAttributes;
                        }
                }
-#endif         
 
                [DefaultValue ("")]
                [Bindable (true)]
-#if NET_2_0
                [Localizable (true)]
-#endif         
                [WebSysDescription ("")]
                [WebCategory ("Appearance")]
-               public virtual string Text 
-               {
-                       get {
-                               return (ViewState.GetString ("Text",
-                                                            String.Empty));
-                       }
-                       set {
-                               ViewState["Text"] = value;
-                       }
+               public virtual string Text {
+                       get { return (ViewState.GetString ("Text", String.Empty)); }
+                       set { ViewState["Text"] = value; }
                }
 
                [DefaultValue (TextAlign.Right)]
-#if ONLY_1_1
-               [Bindable (true)]
-#endif         
                [WebSysDescription ("")]
                [WebCategory ("Appearance")]
-               public virtual TextAlign TextAlign
-               {
+               public virtual TextAlign TextAlign {
                        get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
                        set {
                                if (value != TextAlign.Left &&
@@ -193,48 +149,35 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-#if NET_2_0
                [Themeable (false)]
                [DefaultValue ("")]
                [WebSysDescription ("")]
                [WebCategoryAttribute ("Behavior")]
-               public virtual string ValidationGroup
-               {
+               public virtual string ValidationGroup {
                        get { return ViewState.GetString ("ValidationGroup", String.Empty); }
                        set { ViewState["ValidationGroup"] = value; }
                }
-#endif         
 
-               private static readonly object EventCheckedChanged = new object ();
+               static readonly object EventCheckedChanged = new object ();
                [WebSysDescription ("")]
                [WebCategory ("Action")]
-               public event EventHandler CheckedChanged 
-               {
-                       add {
-                               Events.AddHandler (EventCheckedChanged, value);
-                       }
-                       remove {
-                               Events.RemoveHandler (EventCheckedChanged, value);
-                       }
+               public event EventHandler CheckedChanged  {
+                       add { Events.AddHandler (EventCheckedChanged, value); }
+                       remove { Events.RemoveHandler (EventCheckedChanged, value); }
                }
 
                protected virtual void OnCheckedChanged (EventArgs e)
                {
                        EventHandler handler = (EventHandler)Events[EventCheckedChanged];
                        
-                       if (handler != null) {
+                       if (handler != null)
                                handler (this, e);
-                       }
                }
 
-               internal virtual string NameAttribute 
-               {
-                       get {
-                               return (this.UniqueID);
-                       }
+               internal virtual string NameAttribute {
+                       get { return (this.UniqueID); }
                }
                
-#if NET_2_0
                protected override void LoadViewState (object savedState)
                {
                        if (savedState == null) {
@@ -288,201 +231,185 @@ namespace System.Web.UI.WebControls {
                        if (labelAttributesState != null)
                                labelAttributesState.TrackViewState ();
                }
-#endif         
-
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif         
-               override void OnPreRender (EventArgs e)
+
+               protected internal override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
-
-                       if (Page != null) {
-                               Page.RegisterRequiresPostBack (this);
+                       Page page = Page;
+                       
+                       if (page != null && IsEnabled) {
+                               page.RegisterRequiresPostBack (this);
+                               page.RegisterEnabledControl (this);
                        }
                }
 
                static bool IsInputOrCommonAttr (string attname)
                {
-                       attname = attname.ToUpper (CultureInfo.InvariantCulture);
+                       attname = attname.ToUpper (Helpers.InvariantCulture);
                        switch (attname) {
-                       case "VALUE":
-                       case "CHECKED":
-                       case "SIZE":
-                       case "MAXLENGTH":
-                       case "SRC":
-                       case "ALT":
-                       case "USEMAP":
-                       case "DISABLED":
-                       case "READONLY":
-                       case "ACCEPT":
-                       case "ACCESSKEY":
-                       case "TABINDEX":
-                       case "ONFOCUS":
-                       case "ONBLUR":
-                       case "ONSELECT":
-                       case "ONCHANGE":
-                       case "ONCLICK":
-                       case "ONDBLCLICK":
-                       case "ONMOUSEDOWN":
-                       case "ONMOUSEUP":
-                       case "ONMOUSEOVER":
-                       case "ONMOUSEMOVE":
-                       case "ONMOUSEOUT":
-                       case "ONKEYPRESS":
-                       case "ONKEYDOWN":
-                       case "ONKEYUP":
-                               return true;
-                       default:
-                               return false;
+                               case "VALUE":
+                               case "CHECKED":
+                               case "SIZE":
+                               case "MAXLENGTH":
+                               case "SRC":
+                               case "ALT":
+                               case "USEMAP":
+                               case "DISABLED":
+                               case "READONLY":
+                               case "ACCEPT":
+                               case "ACCESSKEY":
+                               case "TABINDEX":
+                               case "ONFOCUS":
+                               case "ONBLUR":
+                               case "ONSELECT":
+                               case "ONCHANGE":
+                               case "ONCLICK":
+                               case "ONDBLCLICK":
+                               case "ONMOUSEDOWN":
+                               case "ONMOUSEUP":
+                               case "ONMOUSEOVER":
+                               case "ONMOUSEMOVE":
+                               case "ONMOUSEOUT":
+                               case "ONKEYPRESS":
+                               case "ONKEYDOWN":
+                               case "ONKEYUP":
+                                       return true;
+                               default:
+                                       return false;
                        }
                }
 
-               void AddAttributesForSpan (HtmlTextWriter writer)
+               bool AddAttributesForSpan (HtmlTextWriter writer)
                {
-                       ICollection k = Attributes.Keys;
-                       string [] keys = new string [k.Count];
-                       k.CopyTo (keys, 0);
-                       foreach (string key in keys) {
-                               if (!IsInputOrCommonAttr (key))
-                                       continue;
-                               if (common_attrs == null)
-                                       common_attrs = new AttributeCollection (new StateBag ());
-                               common_attrs [key] = Attributes [key];
-                               Attributes.Remove (key);
+                       if (HasAttributes) {
+                               AttributeCollection attributes = Attributes;
+                               ICollection k = attributes.Keys;
+                               string [] keys = new string [k.Count];
+                               k.CopyTo (keys, 0);
+                               foreach (string key in keys) {
+                                       if (!IsInputOrCommonAttr (key))
+                                               continue;
+                                       if (common_attrs == null)
+                                               common_attrs = new AttributeCollection (new StateBag ());
+                                       common_attrs [key] = Attributes [key];
+                                       attributes.Remove (key);
+                               }
+                       
+                               if (attributes.Count > 0) {
+                                       attributes.AddAttributes (writer);
+                                       return true;
+                               }
                        }
-                       Attributes.AddAttributes (writer);
+                       
+                       return false;
                }
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif         
-               override void Render (HtmlTextWriter w)
-               {
-                       if (Page != null)
-                               Page.VerifyRenderingInServerForm (this);
 
+               protected internal override void Render (HtmlTextWriter w)
+               {
+                       Page page = Page;
+                       if (page != null) {
+                               page.VerifyRenderingInServerForm (this);
+                               page.ClientScript.RegisterForEventValidation (UniqueID);
+                       }
+                       
                        bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
-                       if (need_span)
-                               ControlStyle.AddAttributesToRender (w, this);
-
-                       if (!Enabled) {
-                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
+                       bool enabled = IsEnabled;
+                       if (!enabled) {
+                               if (!RenderingCompatibilityLessThan40)
+                                       ControlStyle.PrependCssClass (DisabledCssClass);
+                               else
+                                       w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
                                need_span = true;
                        }
 
+                       if (need_span) {
+                               AddDisplayStyleAttribute (w);
+                               ControlStyle.AddAttributesToRender (w, this);
+                       }
+                       
                        string tt = ToolTip;
-                       if (tt != ""){
+                       if (tt != null && tt.Length > 0){
                                w.AddAttribute ("title", tt);
                                need_span = true;
                        }
 
-                       if (Attributes.Count > 0){
-                               AddAttributesForSpan (w);
-                               Attributes.AddAttributes (w);
+                       if (HasAttributes && AddAttributesForSpan (w))
                                need_span = true;
-                       }
-
+                       
                        if (need_span)
                                w.RenderBeginTag (HtmlTextWriterTag.Span);
 
                        TextAlign align = TextAlign;
                        if (align == TextAlign.Right) {
-                               w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
-                               w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
-                               w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
-                               InternalAddAttributesToRender (w);
-                               if (Checked)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
-
-                               if (AutoPostBack){
-#if NET_2_0
-                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions ()));
-#else
-                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick,
-                                                       Page.ClientScript.GetPostBackEventReference (this, String.Empty));
-#endif
-                                       w.AddAttribute ("language", "javascript");
-                               }
-
-                               if (AccessKey.Length > 0)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
-
-                               if (TabIndex != 0)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
-                                                            TabIndex.ToString (CultureInfo.InvariantCulture));
+                               RenderInput (w, enabled);
+                               RenderLabel (w);
+                       } else {
+                               RenderLabel (w);
+                               RenderInput (w, enabled);
+                       }
 
-                               if (common_attrs != null)
-                                       common_attrs.AddAttributes (w);
-                               w.RenderBeginTag (HtmlTextWriterTag.Input);
+                       if (need_span)
                                w.RenderEndTag ();
-                               string text = Text;
-                               if (text != "") {
-#if NET_2_0
-                                       if (labelAttributes != null)
-                                               labelAttributes.AddAttributes (w);
-#endif
-                                       w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
-                                       w.RenderBeginTag (HtmlTextWriterTag.Label);
-                                       w.Write (text);
-                                       w.RenderEndTag ();
-                               }
-                       } else {
-                               string text = Text;
-                               if (text != "") {
-#if NET_2_0
-                                       if (labelAttributes != null)
-                                               labelAttributes.AddAttributes (w);
-#endif
-                                       w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
-                                       w.RenderBeginTag (HtmlTextWriterTag.Label);
-                                       w.Write (text);
-                                       w.RenderEndTag ();
-                               }
+               }
 
+               void RenderInput (HtmlTextWriter w, bool enabled)
+               {
+                       if (ClientID != null && ClientID.Length > 0)
                                w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
-                               w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
-                               w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
-                               InternalAddAttributesToRender (w);
-                               if (Checked)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
-
-                               if (AutoPostBack){
-#if NET_2_0
-                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions ()));
-#else
-                                       w.AddAttribute (HtmlTextWriterAttribute.Onclick,
-                                                       Page.ClientScript.GetPostBackEventReference (this, String.Empty));
-#endif
-                                       w.AddAttribute ("language", "javascript");
+                       w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
+                       string nameAttr = NameAttribute;
+                       if (nameAttr != null && nameAttr.Length > 0)
+                               w.AddAttribute (HtmlTextWriterAttribute.Name, nameAttr);
+                       InternalAddAttributesToRender (w, enabled);
+                       AddAttributesToRender (w);
+                       
+                       if (Checked)
+                               w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked", false);
+
+                       if (AutoPostBack) {
+                               Page page = Page;
+                               string onclick = page != null ? page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true) : String.Empty;
+                               onclick = String.Concat ("setTimeout('", onclick.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
+                               if (common_attrs != null && common_attrs ["onclick"] != null) {
+                                       onclick = ClientScriptManager.EnsureEndsWithSemicolon (common_attrs ["onclick"]) + onclick;
+                                       common_attrs.Remove ("onclick");
                                }
+                               w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
+                       }
 
-                               if (AccessKey.Length > 0)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
+                       if (AccessKey.Length > 0)
+                               w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
 
-                               if (TabIndex != 0)
-                                       w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
-                                                            TabIndex.ToString (NumberFormatInfo.InvariantInfo));
+                       if (TabIndex != 0)
+                               w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
+                                                        TabIndex.ToString (NumberFormatInfo.InvariantInfo));
 
-                               if (common_attrs != null)
-                                       common_attrs.AddAttributes (w);
-                               w.RenderBeginTag (HtmlTextWriterTag.Input);
-                               w.RenderEndTag ();
-                       }
+                       if (common_attrs != null)
+                               common_attrs.AddAttributes (w);
 
-                       if (need_span)
+                       if (inputAttributes != null)
+                               inputAttributes.AddAttributes (w);
+                       
+                       w.RenderBeginTag (HtmlTextWriterTag.Input);
+                       w.RenderEndTag ();
+               }
+
+               void RenderLabel (HtmlTextWriter w)
+               {
+                       string text = Text;
+                       if (text.Length > 0) {
+                               if (labelAttributes != null)
+                                       labelAttributes.AddAttributes (w);
+                               w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
+                               w.RenderBeginTag (HtmlTextWriterTag.Label);
+                               w.Write (text);
                                w.RenderEndTag ();
+                       }
                }
 
-#if NET_2_0
-               protected virtual
-#endif
-               bool LoadPostData (string postDataKey, NameValueCollection postCollection)
+               protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
                {
-                       if (!Enabled)
+                       if (!IsEnabled)
                                return false;
 
                        string postedValue = postCollection[postDataKey];
@@ -497,15 +424,15 @@ namespace System.Web.UI.WebControls {
                        return (false);
                }
 
-#if NET_2_0
-               protected virtual
-#endif
-               void RaisePostDataChangedEvent ()
+               protected virtual void RaisePostDataChangedEvent ()
                {
-#if NET_2_0
-                       if (CausesValidation)
-                               Page.Validate (ValidationGroup);
-#endif
+                       ValidateEvent (UniqueID, String.Empty);
+                       if (CausesValidation) {
+                               Page page = Page;
+                               if (page != null)
+                                       page.Validate (ValidationGroup);
+                       }
+                       
                        OnCheckedChanged (EventArgs.Empty);
                }
 
@@ -519,15 +446,17 @@ namespace System.Web.UI.WebControls {
                        RaisePostDataChangedEvent ();
                }
 
-#if NET_2_0
-               PostBackOptions GetPostBackOptions () {
+               PostBackOptions GetPostBackOptions ()
+               {
                        PostBackOptions options = new PostBackOptions (this);
                        options.ActionUrl = null;
                        options.ValidationGroup = null;
-                       options.Argument = "";
+                       options.Argument = String.Empty;
                        options.RequiresJavaScriptProtocol = false;
                        options.ClientSubmit = true;
-                       options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
+
+                       Page page = Page;
+                       options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
                        if (options.PerformValidation)
                                options.ValidationGroup = ValidationGroup;
 
@@ -536,14 +465,12 @@ namespace System.Web.UI.WebControls {
 
                protected override void AddAttributesToRender (HtmlTextWriter writer)
                {
-                       base.AddAttributesToRender (writer);
                }
-#endif
 
-               internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
+               internal virtual void InternalAddAttributesToRender (HtmlTextWriter w, bool enabled)
                {
-                       if (!Enabled)
-                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
+                       if (!enabled)
+                               w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
                }
        }
 }