Merge pull request #366 from robwilkens/bug5747-2
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxList.cs
1 //
2 // System.Web.UI.WebControls.CheckBoxList.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Globalization;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33 using System.Web.Util;
34
35 namespace System.Web.UI.WebControls
36 {
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class CheckBoxList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler 
41         {
42                 CheckBox check_box;
43
44                 public CheckBoxList ()
45                 {
46                         check_box = new CheckBox ();
47                         Controls.Add (check_box);
48                 }
49
50                 [DefaultValue(-1)]
51                 [WebSysDescription ("")]
52                 [WebCategory ("Layout")]
53                 public virtual int CellPadding {
54                         get { return TableStyle.CellPadding; }
55                         set { TableStyle.CellPadding = value; }
56                 }
57
58                 [DefaultValue(-1)]
59                 [WebSysDescription ("")]
60                 [WebCategory ("Layout")]
61                 public virtual int CellSpacing {
62                         get { return TableStyle.CellSpacing; }
63                         set { TableStyle.CellSpacing = value; }
64                 }
65
66                 [DefaultValue(0)]
67                 [WebSysDescription ("")]
68                 [WebCategory ("Layout")]
69                 public virtual int RepeatColumns {
70                         get { return ViewState.GetInt ("RepeatColumns", 0); }
71                         set {
72                                 if (value < 0)
73                                         throw new ArgumentOutOfRangeException ("value");
74                                 ViewState ["RepeatColumns"] = value;
75                         }
76                 }
77
78                 [DefaultValue(RepeatDirection.Vertical)]
79                 [WebSysDescription ("")]
80                 [WebCategory ("Layout")]
81                 public virtual RepeatDirection RepeatDirection {
82                         get { return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical); }
83                         set {
84                                 if (value < RepeatDirection.Horizontal ||
85                                                 value > RepeatDirection.Vertical)
86                                         throw new ArgumentOutOfRangeException ("value");
87                                 ViewState ["RepeatDirection"] = value;
88                         }
89                 }
90
91                 [DefaultValue(RepeatLayout.Table)]
92                 [WebSysDescription ("")]
93                 [WebCategory ("Layout")]
94                 public virtual RepeatLayout RepeatLayout {
95                         get { return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table); }
96                         set {
97                                 bool outOfRange;
98 #if NET_4_0
99                                 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.OrderedList;
100 #else
101                                 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.Flow;
102 #endif
103                                 if (outOfRange)
104                                         throw new ArgumentOutOfRangeException ("value");
105                                 ViewState ["RepeatLayout"] = value;
106                         }
107                 }
108
109                 [DefaultValue(TextAlign.Right)]
110                 [WebSysDescription ("")]
111                 [WebCategory ("Appearance")]
112                 public virtual TextAlign TextAlign {
113                         get { return (TextAlign) ViewState.GetInt ("TextAlign", (int) TextAlign.Right); }
114                         set {
115                                 if (value < TextAlign.Left || value > TextAlign.Right)
116                                         throw new ArgumentOutOfRangeException ("value");
117                                 ViewState ["TextAlign"] = value;
118                         }
119                 }
120
121                 TableStyle TableStyle {
122                         get { return (TableStyle) ControlStyle; }
123                 }
124
125                 protected override Style CreateControlStyle ()
126                 {
127                         return new TableStyle (ViewState);
128                 }
129
130                 protected override Control FindControl (string id, int pathOffset)
131                 {
132                         // Always, or in just all my tests?
133                         return this;
134                 }
135
136                 protected internal override void OnPreRender (EventArgs e)
137                 {
138                         base.OnPreRender (e);
139
140                         // Register all of the checked controls so we can
141                         // find out when they are unchecked.
142                         Page page = Page;
143                         for (int i = 0; i < Items.Count; i++) {
144                                 if (Items [i].Selected) {
145                                         check_box.ID = i.ToString (Helpers.InvariantCulture);
146                                         if (page != null)
147                                                 page.RegisterRequiresPostBack (check_box);
148                                 }
149                         }
150                 }
151
152                 protected internal override void Render (HtmlTextWriter writer)
153                 {
154                         if (Items.Count == 0)
155                                 return;
156
157                         RepeatInfo ri = new RepeatInfo ();
158                         ri.RepeatColumns = RepeatColumns;
159                         ri.RepeatDirection = RepeatDirection;
160                         ri.RepeatLayout = RepeatLayout;
161
162                         short ti = 0;
163                         if (TabIndex != 0) {
164                                 check_box.TabIndex = TabIndex;
165                                 ti = TabIndex;
166                                 TabIndex = 0;
167                         }
168
169                         string ak = AccessKey;
170                         check_box.AccessKey = ak;
171                         this.AccessKey = null;
172
173                         ri.RenderRepeater (writer, this, TableStyle, this);
174
175                         if (ti != 0)
176                                 TabIndex = ti;
177                         this.AccessKey = ak;
178                 }
179
180                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
181                 {
182                         if (!IsEnabled)
183                                 return false;
184
185                         EnsureDataBound ();
186                         int checkbox = -1;
187
188                         try {
189                                 string id = postDataKey.Substring (ClientID.Length + 1);
190                                 if (Char.IsDigit (id [0]))
191                                         checkbox = Int32.Parse (id, Helpers.InvariantCulture);
192                         } catch {
193                                 return false;
194                         }
195
196                         if (checkbox == -1)
197                                 return false;
198
199                         string val = postCollection [postDataKey];
200                         bool ischecked = val == "on";
201 #if NET_4_0
202         if(!RenderingCompatibilityLessThan40 && val != null){
203                 ischecked = val == Items[checkbox].Value;
204         }
205 #endif
206                         ListItem item = Items [checkbox];
207                         if (item.Enabled) {
208                                 if (ischecked && !item.Selected) {
209                                         item.Selected = true;
210                                         return true;
211                                 } else if (!ischecked && item.Selected) {
212                                         item.Selected = false;
213                                         return true;
214                                 }
215                         }
216                         
217                         return false;
218                 }
219
220                 protected virtual void RaisePostDataChangedEvent ()
221                 {
222                         if (CausesValidation) {
223                                 Page page = Page;
224                                 if (page != null)
225                                         page.Validate (ValidationGroup);
226                         }
227                         
228                         OnSelectedIndexChanged (EventArgs.Empty);
229                 }
230
231                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
232                 {
233                         return LoadPostData (postDataKey, postCollection);
234                 }
235
236                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
237                 {
238                         RaisePostDataChangedEvent ();
239                 }
240
241                 protected virtual bool HasFooter  {
242                         get { return false; }
243                 }
244
245                 bool IRepeatInfoUser.HasFooter {
246                         get { return HasFooter; }
247                 }
248
249                 protected virtual bool HasHeader {
250                         get { return false; }
251                 }
252
253                 bool IRepeatInfoUser.HasHeader {
254                         get { return HasHeader; }
255                 }
256
257                 protected virtual bool HasSeparators {
258                         get { return false; }
259                 }
260
261                 bool IRepeatInfoUser.HasSeparators {
262                         get { return HasSeparators; }
263                 }
264
265                 protected virtual int RepeatedItemCount {
266                         get { return Items.Count; }
267                 }
268
269                 int IRepeatInfoUser.RepeatedItemCount {
270                         get { return RepeatedItemCount; }
271                 }
272
273                 protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
274                 {
275                         return null;
276                 }
277
278                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
279                 {
280                         return GetItemStyle (itemType, repeatIndex);
281                 }
282
283                 protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
284                 {
285                         ListItem item = Items [repeatIndex];
286
287                         string cssClass = check_box.CssClass;
288                         if (!String.IsNullOrEmpty (cssClass))
289                                 check_box.CssClass = String.Empty;
290                         check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
291                         check_box.Text = item.Text;
292                         check_box.AutoPostBack = AutoPostBack;
293                         check_box.Checked = item.Selected;
294                         check_box.TextAlign = TextAlign;
295                         if (!IsEnabled)
296                                 check_box.Enabled = false;
297                         else
298                                 check_box.Enabled = item.Enabled;
299
300                         check_box.ValidationGroup = ValidationGroup;
301                         check_box.CausesValidation = CausesValidation;
302                         if (check_box.HasAttributes)
303                                 check_box.Attributes.Clear ();
304                         if (item.HasAttributes)
305                                 check_box.Attributes.CopyFrom (item.Attributes);
306 #if NET_4_0
307                         if (!RenderingCompatibilityLessThan40) {
308                                 var attrs = check_box.InputAttributes;
309                         
310                                 attrs.Clear ();
311                                 attrs.Add ("value", item.Value);
312                         }
313 #endif
314                         check_box.RenderControl (writer);
315                 }
316
317                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
318                 {
319                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
320                 }
321
322                 internal override bool MultiSelectOk ()
323                 {
324                         return true;
325                 }
326         }
327 }