2006-08-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxList.cs
index 1a4ae4e8e5fa203603e375a477d13f720877d0f7..35fd1827e3ca31d7963398077bf5f9d82672e8bc 100644 (file)
-/**\r
-* Namespace: System.Web.UI.WebControls\r
-* Class:     CheckBoxList\r
-*\r
-* Author:  Gaurav Vaish\r
-* Maintainer: gvaish@iitk.ac.in\r
-* Contact: <gvaish@iitk.ac.in>\r
-* Implementation: yes\r
-* Status:  100%\r
-*\r
-* (C) Gaurav Vaish (2001)\r
-*/\r
-\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
-       public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler\r
-       {\r
-               CheckBox checkBoxRepeater;\r
-               bool     isChangeNotified;\r
-\r
-               public CheckBoxList()\r
-               {\r
-                       checkBoxRepeater = new CheckBox();\r
-                       checkBoxRepeater.ID = "0";\r
-                       checkBoxRepeater.EnableViewState = false;\r
-                       Controls.Add (checkBoxRepeater);\r
-                       isChangeNotified = false;\r
-               }\r
-\r
-               public virtual int CellPadding\r
-               {\r
-                       get\r
-                       {\r
-                               return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);\r
-                       }\r
-                       set\r
-                       {\r
-                               ((TableStyle)ControlStyle).CellPadding = value;\r
-                       }\r
-               }\r
-\r
-               public virtual int CellSpacing\r
-               {\r
-                       get\r
-                       {\r
-                               return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);\r
-                       }\r
-                       set\r
-                       {\r
-                               ((TableStyle)ControlStyle).CellSpacing = value;\r
-                       }\r
-               }\r
-\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
-                       {\r
-                               if(value < 0)\r
-                                       throw new ArgumentOutOfRangeException();\r
-                               ViewState["RepeatColumns"] = value;\r
-                       }\r
-               }\r
-\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(!System.Enum.IsDefined(typeof(RepeatDirection),value))\r
-                                       throw new ArgumentException();\r
-                               ViewState["RepeatDirection"] = value;\r
-                       }\r
-               }\r
-\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(!System.Enum.IsDefined(typeof(RepeatLayout), value))\r
-                                       throw new ArgumentException();\r
-                               ViewState["RepeatLayout"] = value;\r
-                       }\r
-               }\r
-\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 ArgumentException();\r
-                               ViewState["TextAlign"] = value;\r
-                       }\r
-               }\r
-\r
-               protected override Style CreateControlStyle()\r
-               {\r
-                       return new TableStyle(ViewState);\r
-               }\r
-\r
-               protected override Control FindControl(string id, int pathOffset)\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               protected override void OnPreRender(EventArgs e)\r
-               {\r
-                       checkBoxRepeater.AutoPostBack = AutoPostBack;\r
-                       if(Page!=null)\r
-                       {\r
-                               for(int i=0; i < Items.Count; i++)\r
-                               {\r
-                                       if(Items[i].Selected)\r
-                                       {\r
-                                               checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);\r
-                                               Page.RegisterRequiresPostBack(checkBoxRepeater);\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-\r
-               protected override void Render(HtmlTextWriter writer)\r
-               {\r
-                       RepeatInfo ri = new RepeatInfo();\r
-                       checkBoxRepeater.TabIndex = TabIndex;\r
-                       bool dirtyFlag = false;\r
-                       short  tTabIndex = TabIndex;\r
-                       Style s = (ControlStyleCreated ? ControlStyle : null);\r
-                       if(TabIndex > 0)\r
-                       {\r
-                               if(!ViewState.IsItemDirty("TabIndex"))\r
-                                       dirtyFlag = true;\r
-                               TabIndex = 0;\r
-                       }\r
-                       ri.RepeatColumns = RepeatColumns;\r
-                       ri.RepeatLayout  = RepeatLayout;\r
-                       ri.RepeatDirection = RepeatDirection;\r
-                       ri.RenderRepeater(writer, this, s, this);\r
-                       if(tTabIndex > 0)\r
-                       {\r
-                               TabIndex = tTabIndex;\r
-                       }\r
-                       if(dirtyFlag)\r
-                       {\r
-                               ViewState.SetItemDirty("TabIndex", false);\r
-                       }\r
-               }\r
-\r
-               bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)\r
-               {\r
-                       int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));\r
-                       if(index >= 0 && index < Items.Count)\r
-                       {\r
-                               bool exists = (postCollection[postDataKey]!=null);\r
-                               if(Items[index].Selected != exists)\r
-                               {\r
-                                       Items[index].Selected = exists;\r
-                                       if(!isChangeNotified)\r
-                                       {\r
-                                               isChangeNotified = true;\r
-                                               return true;\r
-                                       }\r
-                               }\r
-                       }\r
-                       return false;\r
-               }\r
-\r
-               void IPostBackDataHandler.RaisePostDataChangedEvent()\r
-               {\r
-                       OnSelectedIndexChanged(EventArgs.Empty);\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
-               Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)\r
-               {\r
-                       checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);\r
-                       checkBoxRepeater.Text = Items[repeatIndex].Text;\r
-                       checkBoxRepeater.TextAlign = TextAlign;\r
-                       checkBoxRepeater.Checked = Items[repeatIndex].Selected;\r
-                       checkBoxRepeater.RenderControl(writer);\r
-               }\r
-       }\r
-}\r
+//
+// System.Web.UI.WebControls.CheckBoxList.cs
+//
+// Authors:
+//     Jackson Harper (jackson@ximian.com)
+//
+// (C) 2005 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
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// 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
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.Globalization;
+using System.Collections.Specialized;
+using System.ComponentModel;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls {
+
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public class CheckBoxList : ListControl, IRepeatInfoUser,
+                                   INamingContainer, IPostBackDataHandler {
+
+               private CheckBox check_box;
+
+               public CheckBoxList ()
+               {
+                       check_box = new CheckBox ();
+                       Controls.Add (check_box);
+               }
+
+#if ONLY_1_1
+               [Bindable(true)]
+#endif         
+               [DefaultValue(-1)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual int CellPadding {
+                       get { return TableStyle.CellPadding; }
+                       set { TableStyle.CellPadding = value; }
+               }
+
+#if ONLY_1_1
+               [Bindable(true)]
+#endif         
+               [DefaultValue(-1)]
+               [WebSysDescription ("")]
+               [WebCategory ("Layout")]
+               public virtual int CellSpacing {
+                       get { return TableStyle.CellSpacing; }
+                       set { TableStyle.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 < 0)
+                                       throw new ArgumentOutOfRangeException ("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 ("value");
+                               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.Table ||
+                                               value > RepeatLayout.Flow)
+                                       throw new ArgumentOutOfRangeException ("value");
+                               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 ("value");
+                               ViewState ["TextAlign"] = value;
+                       }
+               }
+
+               private TableStyle TableStyle {
+                       get { return (TableStyle) ControlStyle; }
+               }
+
+               protected override Style CreateControlStyle ()
+               {
+                       return new TableStyle (ViewState);
+               }
+
+               protected override Control FindControl (string id, int pathOffset)
+               {
+                       // Always, or in just all my tests?
+                       return this;
+               }
+
+#if NET_2_0
+               protected internal
+#else          
+               protected
+#endif         
+               override void OnPreRender (EventArgs e)
+               {
+                       base.OnPreRender (e);
+
+                       // Register all of the checked controls so we can
+                       // find out when they are unchecked.
+                       for (int i = 0; i < Items.Count; i++) {
+                               if (Items [i].Selected) {
+                                       check_box.ID = i.ToString (CultureInfo.InvariantCulture);
+                                       Page.RegisterRequiresPostBack (check_box);
+                               }
+                       }
+               }
+
+#if NET_2_0
+               protected internal
+#else          
+               protected
+#endif         
+               override void Render (HtmlTextWriter writer)
+               {
+#if NET_2_0
+                       if (Items.Count == 0)
+                               return;
+#endif
+                       RepeatInfo ri = new RepeatInfo ();
+                       ri.RepeatColumns = RepeatColumns;
+                       ri.RepeatDirection = RepeatDirection;
+                       ri.RepeatLayout = RepeatLayout;
+
+                       short ti = 0;
+                       if (TabIndex != 0) {
+                               check_box.TabIndex = TabIndex;
+                               ti = TabIndex;
+                               TabIndex = 0;
+                       }
+
+                       ri.RenderRepeater (writer, this, TableStyle, this);
+
+                       if (ti != 0)
+                               TabIndex = ti;
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               bool LoadPostData (string postDataKey, NameValueCollection postCollection)
+               {
+                       int checkbox = -1;
+
+                       try {
+                               string id = postDataKey.Substring (ClientID.Length + 1);
+                               if (Char.IsDigit (id [0]))
+                                       checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
+                       } catch {
+                               return false;
+                       }
+
+                       if (checkbox == -1)
+                               return false;
+
+                       string val = postCollection [postDataKey];
+                       bool ischecked = val == "on";
+                       ListItem item = Items [checkbox];
+
+                       if (item.Selected != ischecked) {
+                               item.Selected = ischecked;
+                               return true;
+                       }
+
+                       return false;
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               void RaisePostDataChangedEvent ()
+               {
+                       OnSelectedIndexChanged (EventArgs.Empty);
+               }
+
+               bool IPostBackDataHandler.LoadPostData (string postDataKey,
+                                                       NameValueCollection postCollection)
+               {
+                       return LoadPostData (postDataKey, postCollection);
+               }
+
+               void IPostBackDataHandler.RaisePostDataChangedEvent ()
+               {
+                       RaisePostDataChangedEvent ();
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               bool HasFooter 
+               {
+                       get {
+                               return false;
+                       }
+               }
+
+               bool IRepeatInfoUser.HasFooter {
+                       get { return HasFooter; }
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               bool HasHeader
+               {
+                       get {
+                               return false;
+                       }
+               }
+
+               bool IRepeatInfoUser.HasHeader {
+                       get { return HasHeader; }
+               }
+
+
+#if NET_2_0
+               protected virtual
+#endif
+               bool HasSeparators
+               {
+                       get {
+                               return false;
+                       }
+               }
+
+               bool IRepeatInfoUser.HasSeparators {
+                       get { return HasSeparators; }
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               int RepeatedItemCount
+               {
+                       get {
+                               return Items.Count;
+                       }
+               }
+
+               int IRepeatInfoUser.RepeatedItemCount {
+                       get { return RepeatedItemCount; }
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               Style GetItemStyle (ListItemType itemType,
+                                   int repeatIndex)
+               {
+                       return null;
+               }
+
+               Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
+                                                   int repeatIndex)
+               {
+                       return GetItemStyle (itemType, repeatIndex);
+               }
+
+#if NET_2_0
+               protected virtual
+#endif
+               void RenderItem (ListItemType itemType,
+                                int repeatIndex,
+                                RepeatInfo repeatInfo,
+                                HtmlTextWriter writer)
+               {
+                       ListItem item = Items [repeatIndex];
+
+                       check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
+                       check_box.Text = item.Text;
+                       check_box.AutoPostBack = AutoPostBack;
+                       check_box.Checked = item.Selected;
+                       check_box.TextAlign = TextAlign;
+                       check_box.Enabled = Enabled;
+                       check_box.RenderControl (writer);
+               }
+
+               void IRepeatInfoUser.RenderItem (ListItemType itemType,
+                                                int repeatIndex, RepeatInfo repeatInfo,
+                                                HtmlTextWriter writer)
+               {
+                       RenderItem (itemType, repeatIndex, repeatInfo, writer);
+               }
+       }
+}