Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[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 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 #if ONLY_1_1
51                 [Bindable(true)]
52 #endif          
53                 [DefaultValue(-1)]
54                 [WebSysDescription ("")]
55                 [WebCategory ("Layout")]
56                 public virtual int CellPadding {
57                         get { return TableStyle.CellPadding; }
58                         set { TableStyle.CellPadding = value; }
59                 }
60
61 #if ONLY_1_1
62                 [Bindable(true)]
63 #endif          
64                 [DefaultValue(-1)]
65                 [WebSysDescription ("")]
66                 [WebCategory ("Layout")]
67                 public virtual int CellSpacing {
68                         get { return TableStyle.CellSpacing; }
69                         set { TableStyle.CellSpacing = value; }
70                 }
71
72 #if ONLY_1_1
73                 [Bindable(true)]
74 #endif          
75                 [DefaultValue(0)]
76                 [WebSysDescription ("")]
77                 [WebCategory ("Layout")]
78                 public virtual int RepeatColumns {
79                         get { return ViewState.GetInt ("RepeatColumns", 0); }
80                         set {
81                                 if (value < 0)
82                                         throw new ArgumentOutOfRangeException ("value");
83                                 ViewState ["RepeatColumns"] = value;
84                         }
85                 }
86
87 #if ONLY_1_1
88                 [Bindable(true)]
89 #endif          
90                 [DefaultValue(RepeatDirection.Vertical)]
91                 [WebSysDescription ("")]
92                 [WebCategory ("Layout")]
93                 public virtual RepeatDirection RepeatDirection {
94                         get {
95                                 return (RepeatDirection) ViewState.GetInt ("RepeatDirection",
96                                                 (int) RepeatDirection.Vertical);
97                         }
98                         set {
99                                 if (value < RepeatDirection.Horizontal ||
100                                                 value > RepeatDirection.Vertical)
101                                         throw new ArgumentOutOfRangeException ("value");
102                                 ViewState ["RepeatDirection"] = value;
103                         }
104                 }
105
106 #if ONLY_1_1
107                 [Bindable(true)]
108 #endif          
109                 [DefaultValue(RepeatLayout.Table)]
110                 [WebSysDescription ("")]
111                 [WebCategory ("Layout")]
112                 public virtual RepeatLayout RepeatLayout {
113                         get {
114                                 return (RepeatLayout) ViewState.GetInt ("RepeatLayout",
115                                                 (int) RepeatLayout.Table);
116                         }
117                         set {
118                                 if (value < RepeatLayout.Table ||
119                                                 value > RepeatLayout.Flow)
120                                         throw new ArgumentOutOfRangeException ("value");
121                                 ViewState ["RepeatLayout"] = value;
122                         }
123                 }
124
125 #if ONLY_1_1
126                 [Bindable(true)]
127 #endif          
128                 [DefaultValue(TextAlign.Right)]
129                 [WebSysDescription ("")]
130                 [WebCategory ("Appearance")]
131                 public virtual TextAlign TextAlign {
132                         get {
133                                 return (TextAlign) ViewState.GetInt ("TextAlign",
134                                                 (int) TextAlign.Right);
135                         }
136                         set {
137                                 if (value < TextAlign.Left || value > TextAlign.Right)
138                                         throw new ArgumentOutOfRangeException ("value");
139                                 ViewState ["TextAlign"] = value;
140                         }
141                 }
142
143                 TableStyle TableStyle {
144                         get { return (TableStyle) ControlStyle; }
145                 }
146
147                 protected override Style CreateControlStyle ()
148                 {
149                         return new TableStyle (ViewState);
150                 }
151
152                 protected override Control FindControl (string id, int pathOffset)
153                 {
154                         // Always, or in just all my tests?
155                         return this;
156                 }
157
158 #if NET_2_0
159                 protected internal
160 #else           
161                 protected
162 #endif          
163                 override void OnPreRender (EventArgs e)
164                 {
165                         base.OnPreRender (e);
166
167                         // Register all of the checked controls so we can
168                         // find out when they are unchecked.
169                         for (int i = 0; i < Items.Count; i++) {
170                                 if (Items [i].Selected) {
171                                         check_box.ID = i.ToString (Helpers.InvariantCulture);
172                                         Page.RegisterRequiresPostBack (check_box);
173                                 }
174                         }
175                 }
176
177 #if NET_2_0
178                 protected internal
179 #else           
180                 protected
181 #endif          
182                 override void Render (HtmlTextWriter writer)
183                 {
184 #if NET_2_0
185                         if (Items.Count == 0)
186                                 return;
187 #endif
188                         RepeatInfo ri = new RepeatInfo ();
189                         ri.RepeatColumns = RepeatColumns;
190                         ri.RepeatDirection = RepeatDirection;
191                         ri.RepeatLayout = RepeatLayout;
192
193                         short ti = 0;
194                         if (TabIndex != 0) {
195                                 check_box.TabIndex = TabIndex;
196                                 ti = TabIndex;
197                                 TabIndex = 0;
198                         }
199 #if NET_2_0
200                         string ak = AccessKey;
201                         check_box.AccessKey = ak;
202                         this.AccessKey = null;
203 #endif
204
205                         ri.RenderRepeater (writer, this, TableStyle, this);
206
207                         if (ti != 0)
208                                 TabIndex = ti;
209 #if NET_2_0
210                         this.AccessKey = ak;
211 #endif
212                 }
213
214 #if NET_2_0
215                 protected virtual
216 #endif
217                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
218                 {
219 #if NET_2_0
220                         EnsureDataBound ();
221 #endif
222                         int checkbox = -1;
223
224                         try {
225                                 string id = postDataKey.Substring (ClientID.Length + 1);
226                                 if (Char.IsDigit (id [0]))
227                                         checkbox = Int32.Parse (id, Helpers.InvariantCulture);
228                         } catch {
229                                 return false;
230                         }
231
232                         if (checkbox == -1)
233                                 return false;
234
235                         string val = postCollection [postDataKey];
236                         bool ischecked = val == "on";
237                         ListItem item = Items [checkbox];
238
239 #if NET_2_0
240                         if (item.Enabled)
241 #endif
242                                 if (ischecked && !item.Selected) {
243                                         item.Selected = true;
244                                         return true;
245                                 }
246
247                         return false;
248                 }
249
250 #if NET_2_0
251                 protected virtual
252 #endif
253                 void RaisePostDataChangedEvent ()
254                 {
255 #if NET_2_0
256                         if (CausesValidation)
257                                 Page.Validate (ValidationGroup);
258 #endif
259                         OnSelectedIndexChanged (EventArgs.Empty);
260                 }
261
262                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
263                                                         NameValueCollection postCollection)
264                 {
265                         return LoadPostData (postDataKey, postCollection);
266                 }
267
268                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
269                 {
270                         RaisePostDataChangedEvent ();
271                 }
272
273 #if NET_2_0
274                 protected virtual
275 #endif
276                 bool HasFooter 
277                 {
278                         get {
279                                 return false;
280                         }
281                 }
282
283                 bool IRepeatInfoUser.HasFooter {
284                         get { return HasFooter; }
285                 }
286
287 #if NET_2_0
288                 protected virtual
289 #endif
290                 bool HasHeader
291                 {
292                         get {
293                                 return false;
294                         }
295                 }
296
297                 bool IRepeatInfoUser.HasHeader {
298                         get { return HasHeader; }
299                 }
300
301
302 #if NET_2_0
303                 protected virtual
304 #endif
305                 bool HasSeparators
306                 {
307                         get {
308                                 return false;
309                         }
310                 }
311
312                 bool IRepeatInfoUser.HasSeparators {
313                         get { return HasSeparators; }
314                 }
315
316 #if NET_2_0
317                 protected virtual
318 #endif
319                 int RepeatedItemCount
320                 {
321                         get {
322                                 return Items.Count;
323                         }
324                 }
325
326                 int IRepeatInfoUser.RepeatedItemCount {
327                         get { return RepeatedItemCount; }
328                 }
329
330 #if NET_2_0
331                 protected virtual
332 #endif
333                 Style GetItemStyle (ListItemType itemType,
334                                     int repeatIndex)
335                 {
336                         return null;
337                 }
338
339                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
340                                                     int repeatIndex)
341                 {
342                         return GetItemStyle (itemType, repeatIndex);
343                 }
344
345 #if NET_2_0
346                 protected virtual
347 #endif
348                 void RenderItem (ListItemType itemType,
349                                  int repeatIndex,
350                                  RepeatInfo repeatInfo,
351                                  HtmlTextWriter writer)
352                 {
353                         ListItem item = Items [repeatIndex];
354
355                         check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
356                         check_box.Text = item.Text;
357                         check_box.AutoPostBack = AutoPostBack;
358                         check_box.Checked = item.Selected;
359                         check_box.TextAlign = TextAlign;
360                         if (!Enabled)
361                                 check_box.Enabled = false;
362                         else
363                                 check_box.Enabled = item.Enabled;
364 #if NET_2_0
365                         check_box.ValidationGroup = ValidationGroup;
366                         check_box.CausesValidation = CausesValidation;
367                         if (check_box.HasAttributes)
368                                 check_box.Attributes.Clear ();
369                         if (item.HasAttributes)
370                                 check_box.Attributes.CopyFrom (item.Attributes);
371 #endif
372                         check_box.RenderControl (writer);
373                 }
374
375                 void IRepeatInfoUser.RenderItem (ListItemType itemType,
376                                                  int repeatIndex, RepeatInfo repeatInfo,
377                                                  HtmlTextWriter writer)
378                 {
379                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
380                 }
381 #if NET_2_0
382         protected internal override void VerifyMultiSelect()
383         {
384             //by default the ListControl will throw an exception in this method,
385             //therefor we should override the method if the class is supporting
386             //MultiSelect option.
387         }
388 #endif
389     }
390 }