New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / RadioButtonList.cs
1 //
2 // System.Web.UI.WebControls.RadioButtonList.cs
3 //
4 // Authors:
5 //    Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //
31
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Collections.Specialized;
35 using System.Security.Permissions;
36
37 namespace System.Web.UI.WebControls {
38
39         // CAS
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         // attributes
43         [ValidationProperty ("SelectedItem")]
44 #if NET_2_0
45         [SupportsEventValidation]
46 #endif
47         public class RadioButtonList : ListControl, IRepeatInfoUser,
48                 INamingContainer, IPostBackDataHandler {
49 #if !NET_2_0
50                 bool need_raise;
51 #endif
52                 short tabIndex = 0;
53
54                 public RadioButtonList ()
55                 {
56
57                 }
58
59 #if ONLY_1_1
60                 [Bindable (true)]
61 #endif          
62                 [DefaultValue (-1)]
63                 [WebSysDescription ("")]
64                 [WebCategory ("Layout")]
65                 public virtual int CellPadding {
66                         get {
67                                 if (ControlStyleCreated == false)
68                                         return -1; // default value
69
70                                 return ((TableStyle) ControlStyle).CellPadding;
71                         }
72
73                         set {
74                                 ((TableStyle) ControlStyle).CellPadding = value;
75                         }
76                 }
77
78 #if ONLY_1_1
79                 [Bindable (true)]
80 #endif          
81                 [DefaultValue (-1)]
82                 [WebSysDescription ("")]
83                 [WebCategory ("Layout")]
84                 public virtual int CellSpacing {
85                         get {
86                                 if (ControlStyleCreated == false)
87                                         return -1; // default value
88
89                                 return ((TableStyle) ControlStyle).CellSpacing;
90                         }
91
92                         set {
93                                 ((TableStyle) ControlStyle).CellSpacing = value;
94                         }
95                 }
96
97 #if ONLY_1_1
98                 [Bindable (true)]
99 #endif          
100                 [DefaultValue (0)]
101                 [WebSysDescription ("")]
102                 [WebCategory ("Layout")]
103                 public virtual int RepeatColumns  {
104                         get {
105                                 return ViewState.GetInt ("RepeatColumns", 0);
106                         }
107
108                         set {
109                                 if (value < 0)
110                                         throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
111
112                                 ViewState ["RepeatColumns"] = value;
113                         }
114                 }
115
116 #if ONLY_1_1
117                 [Bindable (true)]
118 #endif          
119                 [DefaultValue (RepeatDirection.Vertical)]
120                 [WebSysDescription ("")]
121                 [WebCategory ("Layout")]
122                 public virtual RepeatDirection RepeatDirection {
123                         get {
124                                 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
125                         }
126
127                         set {
128                                 if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
129                                         throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
130
131                                 ViewState ["RepeatDirection"] = value;
132                         }
133                 }
134
135 #if ONLY_1_1
136                 [Bindable (true)]
137 #endif          
138                 [DefaultValue (RepeatLayout.Table)]
139                 [WebSysDescription ("")]
140                 [WebCategory ("Layout")]
141                 public virtual RepeatLayout RepeatLayout {
142                         get {
143                                 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
144                         }
145
146                         set {
147                                 if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
148                                         throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
149
150                                 ViewState ["RepeatLayout"] = value;
151                         }
152                 }
153
154 #if ONLY_1_1
155                 [Bindable (true)]
156 #endif          
157                 [DefaultValue (TextAlign.Right)]
158                 [WebSysDescription ("")]
159                 [WebCategory ("Appearance")]
160                 public virtual TextAlign TextAlign {
161                         get {
162                                 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
163                         }
164
165                         set {
166                                 if (value != TextAlign.Left && value != TextAlign.Right)
167                                         throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
168
169                                 ViewState ["TextAlign"] = value;
170                         }
171                 }
172
173                 // Interface properties
174
175 #if NET_2_0
176                 [MonoTODO ("Not implemented")]
177                 protected virtual bool HasFooter
178                 {
179                         get {
180                                 throw new NotImplementedException ();
181                         }
182                 }
183
184                 [MonoTODO ("Not implemented")]
185                 protected virtual bool HasHeader 
186                 {
187                         get {
188                                 throw new NotImplementedException ();
189                         }
190                 }
191
192                 [MonoTODO]
193                 protected virtual bool HasSeparators
194                 {
195                         get {
196                                 throw new NotImplementedException ();
197                         }
198                 }
199                 
200                 [MonoTODO ("Not implemented")]
201                 protected virtual int RepeatedItemCount
202                 {
203                         get {
204                                 throw new NotImplementedException ();
205                         }
206                 }
207 #endif
208                 
209                 bool IRepeatInfoUser.HasFooter {
210                         get { return false; }
211                 }
212
213                 bool IRepeatInfoUser.HasHeader {
214                         get { return false; }
215                 }
216
217                 bool IRepeatInfoUser.HasSeparators {
218                         get { return false; }
219                 }
220
221                 int IRepeatInfoUser.RepeatedItemCount {
222                         get { return Items.Count; }
223                 }
224
225                 protected override Style CreateControlStyle ()
226                 {
227                         return new TableStyle (ViewState);
228                 }
229
230 #if NET_2_0
231                 [MonoTODO ("Not implemented")]
232                 protected override Control FindControl (string id, int pathOffset)
233                 {
234                         throw new NotImplementedException ();
235                 }
236 #endif
237
238 #if NET_2_0
239                 protected virtual
240 #endif
241                 Style GetItemStyle (ListItemType itemType, int repeatIndex)
242                 {
243                         return null;
244                 }
245
246 #if NET_2_0
247                 protected virtual
248 #endif
249                 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
250                 {
251                         RadioButton radio = new RadioButton ();
252                         radio.Text = Items [repeatIndex].Text;
253                         radio.ID = ClientID + "_"  + repeatIndex;
254                         radio.TextAlign = TextAlign;
255                         radio.GroupName = UniqueID;
256                         radio.Page = Page;
257                         radio.Checked = Items [repeatIndex].Selected;
258                         radio.ValueAttribute = Items [repeatIndex].Value;
259                         radio.AutoPostBack = AutoPostBack;
260                         radio.Enabled = Enabled;
261                         radio.TabIndex = tabIndex;
262 #if NET_2_0
263                         radio.ValidationGroup = ValidationGroup;
264                         radio.CausesValidation = CausesValidation;
265 #endif
266                         radio.RenderControl (writer);
267                 }
268 #if NET_2_0
269                 protected virtual
270 #endif
271                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
272                 {
273                         string val = postCollection [postDataKey];
274                         ListItemCollection items = Items;
275                         int end = items.Count;
276                         int selected = SelectedIndex;
277                         for (int i = 0; i < end; i++) {
278                                 ListItem item = items [i];
279                                 if (item == null || val != item.Value)
280                                         continue;
281
282                                 if (i != selected) {
283                                         SelectedIndex = i;
284 #if NET_2_0
285                                         return true;
286 #else
287                                         need_raise = true;
288 #endif
289                                 }
290 #if !NET_2_0
291                                 return true;
292 #endif
293                         }
294
295                         return false;
296                 }
297
298 #if NET_2_0
299                 protected virtual
300 #endif
301                 void RaisePostDataChangedEvent ()
302                 {
303 #if NET_2_0
304                         if (CausesValidation)
305                                 Page.Validate (ValidationGroup);
306 #endif
307
308 #if !NET_2_0
309                         if (need_raise)
310 #endif
311                                 OnSelectedIndexChanged (EventArgs.Empty);
312                 }
313
314                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
315                 {
316                         return LoadPostData (postDataKey, postCollection);
317                 }
318                 
319                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
320                 {
321                         RaisePostDataChangedEvent ();
322                 }
323
324                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,  int repeatIndex)
325                 {
326                         return GetItemStyle (itemType, repeatIndex);
327                 }
328
329                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
330                 {
331                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
332                 }
333
334 #if NET_2_0
335                 protected internal
336 #else           
337                 protected
338 #endif          
339                 override void Render (HtmlTextWriter writer)
340                 {
341 #if NET_2_0
342                         if (Page != null)
343                                 Page.ClientScript.RegisterForEventValidation (this.UniqueID);
344
345                         if (Items.Count == 0)
346                                 return;
347 #endif
348                         RepeatInfo repeat = new RepeatInfo ();
349                         repeat.RepeatColumns = RepeatColumns;
350                         repeat.RepeatDirection = RepeatDirection;
351                         repeat.RepeatLayout = RepeatLayout;
352
353                         tabIndex = TabIndex;
354                         TabIndex = 0;
355
356                         repeat.RenderRepeater (writer, this, ControlStyle, this);
357
358                         TabIndex = tabIndex;
359                 }
360         }
361
362 }
363
364