refactoring: Button, ImageButton and LinkButton are used insted internal DataControlB...
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / RadioButtonList.cs
index 19078dfe5212a415ac297fbaf13be956e8e11c44..7a9d711f12b54e07b2c2cd123328b9d0487ceff9 100644 (file)
@@ -2,13 +2,11 @@
 // System.Web.UI.WebControls.RadioButtonList.cs
 //
 // Authors:
-//   Gaurav Vaish (gvaish@iitk.ac.in)
-//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//    Jordi Mas i Hernandez (jordi@ximian.com)
+//
+// (C) 2005 Novell, Inc (http://www.novell.com)
 //
-// (C) Gaurav Vaish (2002)
-// (C) 2003 Andreas Nahr
 //
-
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // distribute, sublicense, and/or sell copies of the Software, and to
 // permit persons to whom the Software is furnished to do so, subject to
 // the following conditions:
-// 
+//
 // The above copyright notice and this permission notice shall be
 // included in all copies or substantial portions of the Software.
-// 
+//
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-\r
-using System;\r
-using System.Collections.Specialized;\r
-using System.ComponentModel;\r
-using System.Globalization;\r
-using System.Web;\r
-using System.Web.UI;\r
-\r
-namespace System.Web.UI.WebControls\r
-{\r
-       [ValidationProperty("SelectedItem")]\r
-       public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler\r
-       {\r
-               private bool  selectionIndexChanged;\r
-               private short  tabIndex;\r
-\r
-               public RadioButtonList(): base()\r
-               {\r
-                       selectionIndexChanged = false;\r
-               }\r
-
-               [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
-               [WebSysDescription ("The border left within a RadioButton.")]\r
-               public virtual int CellPadding\r
-               {\r
-                       get\r
-                       {\r
-                               if(ControlStyleCreated)\r
-                               {\r
-                                       return (int)(((TableStyle)ControlStyle).CellPadding);\r
-                               }\r
-                               return -1;\r
-                       }\r
+//
+
+using System.Collections;
+using System.ComponentModel;
+using System.Collections.Specialized;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls {
+
+       // CAS
+       [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       // attributes
+       [ValidationProperty ("SelectedItem")]
+#if NET_2_0
+       [SupportsEventValidation]
+#endif
+       public class RadioButtonList : ListControl, IRepeatInfoUser,
+               INamingContainer, IPostBackDataHandler {
+               bool need_raise;
+               short tabIndex = 0;
+
+               public RadioButtonList ()
+               {
+
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (-1)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual int CellPadding {
+                       get {
+                               if (ControlStyleCreated == false)
+                                       return -1; // default value
+
+                               return ((TableStyle) ControlStyle).CellPadding;
+                       }
+
+                       set {
+                               ((TableStyle) ControlStyle).CellPadding = value;
+                       }
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (-1)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual int CellSpacing {
+                       get {
+                               if (ControlStyleCreated == false)
+                                       return -1; // default value
+
+                               return ((TableStyle) ControlStyle).CellSpacing;
+                       }
+
                        set {
-                               if (value < -1)
-                                       throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");\r
-                               ((TableStyle)ControlStyle).CellPadding = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
-               [WebSysDescription ("The border left between RadioButtons.")]\r
-               public virtual int CellSpacing\r
-               {\r
-                       get\r
-                       {\r
-                               if(ControlStyleCreated)\r
-                               {\r
-                                       return (int)(((TableStyle)ControlStyle).CellSpacing);\r
-                               }\r
-                               return -1;\r
-                       }\r
+                               ((TableStyle) ControlStyle).CellSpacing = value;
+                       }
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (0)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual int RepeatColumns  {
+                       get {
+                               return ViewState.GetInt ("RepeatColumns", 0);
+                       }
+
                        set {
-                               if (value < -1)
-                                       throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");\r
-                               ((TableStyle)ControlStyle).CellSpacing = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
-               [WebSysDescription ("The number of columns that should be used to display the RadioButtons.")]\r
-               public virtual int RepeatColumns\r
-               {\r
-                       get\r
-                       {\r
-                               object o = ViewState["RepeatColumns"];\r
-                               if(o != null)\r
-                                       return (int)o;\r
-                               return 0;\r
-                       }\r
-                       set {\r
-                               if (value < 0)\r
-                                       throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");\r
-                               ViewState["RepeatColumns"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
-               [WebSysDescription ("The direction that is followed when doing the layout.")]\r
-               public virtual RepeatDirection RepeatDirection\r
-               {\r
-                       get\r
-                       {\r
-                               object o = ViewState["RepeatDirection"];\r
-                               if(o != null)\r
-                                       return (RepeatDirection)o;\r
-                               return RepeatDirection.Vertical;\r
-                       }\r
-                       set\r
-                       {\r
-                               if(!Enum.IsDefined(typeof(RepeatDirection), value))\r
-                                       throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");\r
-                               ViewState["RepeatDirection"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
-               [WebSysDescription ("The method used to create the layout.")]\r
-               public virtual RepeatLayout RepeatLayout\r
-               {\r
-                       get\r
-                       {\r
-                               object o = ViewState["RepeatLayout"];\r
-                               if(o != null)\r
-                                       return (RepeatLayout)o;\r
-                               return RepeatLayout.Table;\r
-                       }\r
-                       set\r
-                       {\r
-                               if(!Enum.IsDefined(typeof(RepeatLayout), value))\r
-                                       throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");\r
-                               ViewState["RepeatLayout"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
-               [WebSysDescription ("The alignment of the RadioButton text.")]\r
-               public virtual TextAlign TextAlign\r
-               {\r
-                       get\r
-                       {\r
-                               object o = ViewState["TextAlign"];\r
-                               if(o != null)\r
-                                       return (TextAlign)o;\r
-                               return TextAlign.Right;\r
-                       }\r
-                       set\r
-                       {\r
-                               if(!Enum.IsDefined(typeof(TextAlign), value))\r
-                                       throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");\r
-                               ViewState["TextAlign"] = value;\r
-                       }\r
-               }\r
-\r
-               protected override Style CreateControlStyle()\r
-               {\r
-                       return new TableStyle(ViewState);\r
-               }\r
-\r
-               protected override void Render(HtmlTextWriter writer)\r
-               {\r
-                       RepeatInfo info = new RepeatInfo();\r
-                       Style cStyle = (ControlStyleCreated ? ControlStyle : null);\r
-                       bool dirty = false;\r
-                       tabIndex = TabIndex;\r
-                       if(tabIndex != 0)\r
-                       {\r
-                               dirty = !ViewState.IsItemDirty("TabIndex");\r
-                               TabIndex = 0;\r
-                       }\r
-                       info.RepeatColumns = RepeatColumns;\r
-                       info.RepeatDirection = RepeatDirection;\r
-                       info.RepeatLayout = RepeatLayout;\r
-                       info.RenderRepeater(writer, this, cStyle, this);\r
-                       if(tabIndex != 0)\r
-                       {\r
-                               TabIndex = tabIndex;\r
-                       }\r
-                       if(dirty)\r
-                       {\r
-                               ViewState.SetItemDirty("TabIndex", false);\r
-                       }\r
-               }\r
-
-               bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
+                               if (value < 0)
+                                       throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
+
+                               ViewState ["RepeatColumns"] = value;
+                       }
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (RepeatDirection.Vertical)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual RepeatDirection RepeatDirection {
+                       get {
+                               return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
+                       }
+
+                       set {
+                               if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
+                                       throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
+
+                               ViewState ["RepeatDirection"] = value;
+                       }
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (RepeatLayout.Table)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual RepeatLayout RepeatLayout {
+                       get {
+                               return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
+                       }
+
+                       set {
+                               if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
+                                       throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
+
+                               ViewState ["RepeatLayout"] = value;
+                       }
+               }
+
+#if ONLY_1_1
+               [Bindable (true)]
+#endif         
+               [DefaultValue (TextAlign.Right)]
+               [WebSysDescription ("")]
+               [WebCategory ("Appearance")]
+               public virtual TextAlign TextAlign {
+                       get {
+                               return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
+                       }
+
+                       set {
+                               if (value != TextAlign.Left && value != TextAlign.Right)
+                                       throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
+
+                               ViewState ["TextAlign"] = value;
+                       }
+               }
+
+               // Interface properties
+
+#if NET_2_0
+               [MonoTODO]
+               protected virtual bool HasFooter
                {
-                       string value = postCollection [postDataKey];
-                       int c = Items.Count;
-                       for (int i = 0; i < c; i++) {
-                               if (Items [i].Value != value)
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+
+               [MonoTODO]
+               protected virtual bool HasHeader 
+               {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+
+               [MonoTODO]
+               protected virtual bool HasSeparators
+               {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+               
+               [MonoTODO]
+               protected virtual int RepeatedItemCount
+               {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+#endif
+               
+               bool IRepeatInfoUser.HasFooter {
+                       get { return false; }
+               }
+
+               bool IRepeatInfoUser.HasHeader {
+                       get { return false; }
+               }
+
+               bool IRepeatInfoUser.HasSeparators {
+                       get { return false; }
+               }
+
+               int IRepeatInfoUser.RepeatedItemCount {
+                       get { return Items.Count; }
+               }
+
+               protected override Style CreateControlStyle ()
+               {
+                       return new TableStyle (ViewState);
+               }
+
+#if NET_2_0
+               [MonoTODO]
+               protected override Control FindControl (string id, int pathOffset)
+               {
+                       throw new NotImplementedException ();
+               }
+#endif
+
+#if NET_2_0
+               protected virtual
+#endif
+               Style GetItemStyle (ListItemType itemType, int repeatIndex)
+               {
+                       return null;
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
+               {
+                       RadioButton radio = new RadioButton ();
+                       radio.Text = Items [repeatIndex].Text;
+                       radio.ID = ClientID + "_"  + repeatIndex;
+                       radio.TextAlign = TextAlign;
+                       radio.GroupName = UniqueID;
+                       radio.Page = Page;
+                       radio.Checked = Items [repeatIndex].Selected;
+                       radio.ValueAttribute = Items [repeatIndex].Value;
+                       radio.AutoPostBack = AutoPostBack;
+                       radio.Enabled = Enabled;
+                       radio.TabIndex = tabIndex;
+                       radio.RenderControl (writer);
+               }
+#if NET_2_0
+               protected virtual
+#endif
+               bool LoadPostData (string postDataKey, NameValueCollection postCollection)
+               {
+                       string val = postCollection [postDataKey];
+                       ListItemCollection items = Items;
+                       int end = items.Count;
+                       int selected = SelectedIndex;
+                       for (int i = 0; i < end; i++) {
+                               ListItem item = items [i];
+                               if (item == null || val != item.Value)
                                        continue;
 
-                               if (i != SelectedIndex) {
+                               if (i != selected) {
                                        SelectedIndex = i;
-                                       selectionIndexChanged = true;
+                                       need_raise = true;
                                }
-
                                return true;
                        }
 
                        return false;
                }
 
-               void IPostBackDataHandler.RaisePostDataChangedEvent()\r
-               {\r
-                       if(selectionIndexChanged)\r
-                               OnSelectedIndexChanged(EventArgs.Empty);\r
-               }\r
-\r
-               Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,\r
-                                                int repeatIndex,\r
-                                                RepeatInfo repeatInfo,\r
-                                                HtmlTextWriter writer)\r
-               {\r
-                       /* Create a new RadioButton as if it was defined in the page and render it */\r
-                       RadioButton button = new RadioButton ();\r
-                       button.Page = Page;
-                       button.GroupName = UniqueID;\r
-                       button.TextAlign = TextAlign;\r
-                       button.AutoPostBack = AutoPostBack;\r
-                       button.ID = ClientID + "_" + repeatIndex.ToString (NumberFormatInfo.InvariantInfo);;\r
-                       button.TabIndex = tabIndex;
-                       ListItem current = Items [repeatIndex];\r
-                       button.Text = current.Text;\r
-                       button.Attributes ["value"] = current.Value;\r
-                       button.Checked = current.Selected;\r
-                       button.Enabled = Enabled;\r
-                       button.RenderControl (writer);\r
-               }\r
-\r
-               bool IRepeatInfoUser.HasFooter\r
-               {\r
-                       get\r
-                       {\r
-                               return false;\r
-                       }\r
-               }\r
-\r
-               bool IRepeatInfoUser.HasHeader\r
-               {\r
-                       get\r
-                       {\r
-                               return false;\r
-                       }\r
-               }\r
-\r
-               bool IRepeatInfoUser.HasSeparators\r
-               {\r
-                       get\r
-                       {\r
-                               return false;\r
-                       }\r
-               }\r
-\r
-               int IRepeatInfoUser.RepeatedItemCount\r
-               {\r
-                       get\r
-                       {\r
-                               return Items.Count;\r
-                       }\r
-               }\r
-       }\r
-}\r
+#if NET_2_0
+               protected virtual
+#endif
+               void RaisePostDataChangedEvent ()
+               {
+                       if (need_raise)
+                               OnSelectedIndexChanged (EventArgs.Empty);
+               }
+
+               bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
+               {
+                       return LoadPostData (postDataKey, postCollection);
+               }
+               
+               void IPostBackDataHandler.RaisePostDataChangedEvent ()
+               {
+                       RaisePostDataChangedEvent ();
+               }
+
+               Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,  int repeatIndex)
+               {
+                       return GetItemStyle (itemType, repeatIndex);
+               }
+
+               void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
+               {
+                       RenderItem (itemType, repeatIndex, repeatInfo, writer);
+               }
+
+#if NET_2_0
+               protected internal
+#else          
+               protected
+#endif         
+               override void Render (HtmlTextWriter writer)
+               {
+                       RepeatInfo repeat = new RepeatInfo ();
+                       repeat.RepeatColumns = RepeatColumns;
+                       repeat.RepeatDirection = RepeatDirection;
+                       repeat.RepeatLayout = RepeatLayout;
+
+                       tabIndex = TabIndex;
+                       TabIndex = 0;
+
+                       repeat.RenderRepeater (writer, this, ControlStyle, this);
+
+                       TabIndex = tabIndex;
+               }
+       }
+
+}
+