New test.
[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                         int checkbox = -1;
212
213                         try {
214                                 string id = postDataKey.Substring (ClientID.Length + 1);
215                                 if (Char.IsDigit (id [0]))
216                                         checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
217                         } catch {
218                                 return false;
219                         }
220
221                         if (checkbox == -1)
222                                 return false;
223
224                         string val = postCollection [postDataKey];
225                         bool ischecked = val == "on";
226                         ListItem item = Items [checkbox];
227
228                         if (item.Selected != ischecked) {
229                                 item.Selected = ischecked;
230                                 return true;
231                         }
232
233                         return false;
234                 }
235
236 #if NET_2_0
237                 protected virtual
238 #endif
239                 void RaisePostDataChangedEvent ()
240                 {
241 #if NET_2_0
242                         if (CausesValidation)
243                                 Page.Validate (ValidationGroup);
244 #endif
245                         OnSelectedIndexChanged (EventArgs.Empty);
246                 }
247
248                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
249                                                         NameValueCollection postCollection)
250                 {
251                         return LoadPostData (postDataKey, postCollection);
252                 }
253
254                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
255                 {
256                         RaisePostDataChangedEvent ();
257                 }
258
259 #if NET_2_0
260                 protected virtual
261 #endif
262                 bool HasFooter 
263                 {
264                         get {
265                                 return false;
266                         }
267                 }
268
269                 bool IRepeatInfoUser.HasFooter {
270                         get { return HasFooter; }
271                 }
272
273 #if NET_2_0
274                 protected virtual
275 #endif
276                 bool HasHeader
277                 {
278                         get {
279                                 return false;
280                         }
281                 }
282
283                 bool IRepeatInfoUser.HasHeader {
284                         get { return HasHeader; }
285                 }
286
287
288 #if NET_2_0
289                 protected virtual
290 #endif
291                 bool HasSeparators
292                 {
293                         get {
294                                 return false;
295                         }
296                 }
297
298                 bool IRepeatInfoUser.HasSeparators {
299                         get { return HasSeparators; }
300                 }
301
302 #if NET_2_0
303                 protected virtual
304 #endif
305                 int RepeatedItemCount
306                 {
307                         get {
308                                 return Items.Count;
309                         }
310                 }
311
312                 int IRepeatInfoUser.RepeatedItemCount {
313                         get { return RepeatedItemCount; }
314                 }
315
316 #if NET_2_0
317                 protected virtual
318 #endif
319                 Style GetItemStyle (ListItemType itemType,
320                                     int repeatIndex)
321                 {
322                         return null;
323                 }
324
325                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
326                                                     int repeatIndex)
327                 {
328                         return GetItemStyle (itemType, repeatIndex);
329                 }
330
331 #if NET_2_0
332                 protected virtual
333 #endif
334                 void RenderItem (ListItemType itemType,
335                                  int repeatIndex,
336                                  RepeatInfo repeatInfo,
337                                  HtmlTextWriter writer)
338                 {
339                         ListItem item = Items [repeatIndex];
340
341                         check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
342                         check_box.Text = item.Text;
343                         check_box.AutoPostBack = AutoPostBack;
344                         check_box.Checked = item.Selected;
345                         check_box.TextAlign = TextAlign;
346                         check_box.Enabled = Enabled;
347 #if NET_2_0
348                         check_box.ValidationGroup = ValidationGroup;
349                         check_box.CausesValidation = CausesValidation;
350 #endif
351                         check_box.RenderControl (writer);
352                 }
353
354                 void IRepeatInfoUser.RenderItem (ListItemType itemType,
355                                                  int repeatIndex, RepeatInfo repeatInfo,
356                                                  HtmlTextWriter writer)
357                 {
358                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
359                 }
360         }
361 }