2007-03-05 Igor Zelmanovich <igorz@mainsoft.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
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public class CheckBoxList : ListControl, IRepeatInfoUser,
40                                     INamingContainer, IPostBackDataHandler {
41
42                 private 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                 private 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 (CultureInfo.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
200                         ri.RenderRepeater (writer, this, TableStyle, this);
201
202                         if (ti != 0)
203                                 TabIndex = ti;
204                 }
205
206 #if NET_2_0
207                 protected virtual
208 #endif
209                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
210                 {
211 #if NET_2_0
212                         EnsureDataBound ();
213 #endif
214                         int checkbox = -1;
215
216                         try {
217                                 string id = postDataKey.Substring (ClientID.Length + 1);
218                                 if (Char.IsDigit (id [0]))
219                                         checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
220                         } catch {
221                                 return false;
222                         }
223
224                         if (checkbox == -1)
225                                 return false;
226
227                         string val = postCollection [postDataKey];
228                         bool ischecked = val == "on";
229                         ListItem item = Items [checkbox];
230
231                         if (item.Selected != ischecked) {
232                                 item.Selected = ischecked;
233                                 return true;
234                         }
235
236                         return false;
237                 }
238
239 #if NET_2_0
240                 protected virtual
241 #endif
242                 void RaisePostDataChangedEvent ()
243                 {
244 #if NET_2_0
245                         if (CausesValidation)
246                                 Page.Validate (ValidationGroup);
247 #endif
248                         OnSelectedIndexChanged (EventArgs.Empty);
249                 }
250
251                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
252                                                         NameValueCollection postCollection)
253                 {
254                         return LoadPostData (postDataKey, postCollection);
255                 }
256
257                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
258                 {
259                         RaisePostDataChangedEvent ();
260                 }
261
262 #if NET_2_0
263                 protected virtual
264 #endif
265                 bool HasFooter 
266                 {
267                         get {
268                                 return false;
269                         }
270                 }
271
272                 bool IRepeatInfoUser.HasFooter {
273                         get { return HasFooter; }
274                 }
275
276 #if NET_2_0
277                 protected virtual
278 #endif
279                 bool HasHeader
280                 {
281                         get {
282                                 return false;
283                         }
284                 }
285
286                 bool IRepeatInfoUser.HasHeader {
287                         get { return HasHeader; }
288                 }
289
290
291 #if NET_2_0
292                 protected virtual
293 #endif
294                 bool HasSeparators
295                 {
296                         get {
297                                 return false;
298                         }
299                 }
300
301                 bool IRepeatInfoUser.HasSeparators {
302                         get { return HasSeparators; }
303                 }
304
305 #if NET_2_0
306                 protected virtual
307 #endif
308                 int RepeatedItemCount
309                 {
310                         get {
311                                 return Items.Count;
312                         }
313                 }
314
315                 int IRepeatInfoUser.RepeatedItemCount {
316                         get { return RepeatedItemCount; }
317                 }
318
319 #if NET_2_0
320                 protected virtual
321 #endif
322                 Style GetItemStyle (ListItemType itemType,
323                                     int repeatIndex)
324                 {
325                         return null;
326                 }
327
328                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
329                                                     int repeatIndex)
330                 {
331                         return GetItemStyle (itemType, repeatIndex);
332                 }
333
334 #if NET_2_0
335                 protected virtual
336 #endif
337                 void RenderItem (ListItemType itemType,
338                                  int repeatIndex,
339                                  RepeatInfo repeatInfo,
340                                  HtmlTextWriter writer)
341                 {
342                         ListItem item = Items [repeatIndex];
343
344                         check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
345                         check_box.Text = item.Text;
346                         check_box.AutoPostBack = AutoPostBack;
347                         check_box.Checked = item.Selected;
348                         check_box.TextAlign = TextAlign;
349                         check_box.Enabled = Enabled;
350 #if NET_2_0
351                         check_box.ValidationGroup = ValidationGroup;
352                         check_box.CausesValidation = CausesValidation;
353                         if (check_box.HasAttributes)
354                                 check_box.Attributes.Clear ();
355                         if (item.HasAttributes)
356                                 check_box.Attributes.CopyFrom (item.Attributes);
357 #endif
358                         check_box.RenderControl (writer);
359                 }
360
361                 void IRepeatInfoUser.RenderItem (ListItemType itemType,
362                                                  int repeatIndex, RepeatInfo repeatInfo,
363                                                  HtmlTextWriter writer)
364                 {
365                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
366                 }
367 #if NET_2_0
368         protected internal override void VerifyMultiSelect()
369         {
370             //by default the ListControl will throw an exception in this method,
371             //therefor we should override the method if the class is supporting
372             //MultiSelect option.
373         }
374 #endif
375     }
376 }