cb77e457756013505160040ce5c4372f93b3ec9b
[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                 bool need_raise;
50
51                 public RadioButtonList ()
52                 {
53
54                 }
55
56 #if ONLY_1_1
57                 [Bindable (true)]
58 #endif          
59                 [DefaultValue (-1)]
60                 [WebSysDescription ("")]
61                 [WebCategory ("Layout")]
62                 public virtual int CellPadding {
63                         get {
64                                 if (ControlStyleCreated == false)
65                                         return -1; // default value
66
67                                 return ((TableStyle) ControlStyle).CellPadding;
68                         }
69
70                         set {
71                                 ((TableStyle) ControlStyle).CellPadding = value;
72                         }
73                 }
74
75 #if ONLY_1_1
76                 [Bindable (true)]
77 #endif          
78                 [DefaultValue (-1)]
79                 [WebSysDescription ("")]
80                 [WebCategory ("Layout")]
81                 public virtual int CellSpacing {
82                         get {
83                                 if (ControlStyleCreated == false)
84                                         return -1; // default value
85
86                                 return ((TableStyle) ControlStyle).CellSpacing;
87                         }
88
89                         set {
90                                 ((TableStyle) ControlStyle).CellSpacing = value;
91                         }
92                 }
93
94 #if ONLY_1_1
95                 [Bindable (true)]
96 #endif          
97                 [DefaultValue (0)]
98                 [WebSysDescription ("")]
99                 [WebCategory ("Layout")]
100                 public virtual int RepeatColumns  {
101                         get {
102                                 return ViewState.GetInt ("RepeatColumns", 0);
103                         }
104
105                         set {
106                                 if (value < 0)
107                                         throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
108
109                                 ViewState ["RepeatColumns"] = value;
110                         }
111                 }
112
113 #if ONLY_1_1
114                 [Bindable (true)]
115 #endif          
116                 [DefaultValue (RepeatDirection.Vertical)]
117                 [WebSysDescription ("")]
118                 [WebCategory ("Layout")]
119                 public virtual RepeatDirection RepeatDirection {
120                         get {
121                                 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
122                         }
123
124                         set {
125                                 if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
126                                         throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
127
128                                 ViewState ["RepeatDirection"] = value;
129                         }
130                 }
131
132 #if ONLY_1_1
133                 [Bindable (true)]
134 #endif          
135                 [DefaultValue (RepeatLayout.Table)]
136                 [WebSysDescription ("")]
137                 [WebCategory ("Layout")]
138                 public virtual RepeatLayout RepeatLayout {
139                         get {
140                                 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
141                         }
142
143                         set {
144                                 if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
145                                         throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
146
147                                 ViewState ["RepeatLayout"] = value;
148                         }
149                 }
150
151 #if ONLY_1_1
152                 [Bindable (true)]
153 #endif          
154                 [DefaultValue (TextAlign.Right)]
155                 [WebSysDescription ("")]
156                 [WebCategory ("Appearance")]
157                 public virtual TextAlign TextAlign {
158                         get {
159                                 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
160                         }
161
162                         set {
163                                 if (value != TextAlign.Left && value != TextAlign.Right)
164                                         throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
165
166                                 ViewState ["TextAlign"] = value;
167                         }
168                 }
169
170                 // Interface properties
171
172 #if NET_2_0
173                 [MonoTODO]
174                 protected virtual bool HasFooter
175                 {
176                         get {
177                                 throw new NotImplementedException ();
178                         }
179                 }
180
181                 [MonoTODO]
182                 protected virtual bool HasHeader 
183                 {
184                         get {
185                                 throw new NotImplementedException ();
186                         }
187                 }
188
189                 [MonoTODO]
190                 protected virtual bool HasSeparators
191                 {
192                         get {
193                                 throw new NotImplementedException ();
194                         }
195                 }
196                 
197                 [MonoTODO]
198                 protected virtual int RepeatedItemCount
199                 {
200                         get {
201                                 throw new NotImplementedException ();
202                         }
203                 }
204 #endif
205                 
206                 bool IRepeatInfoUser.HasFooter {
207                         get { return false; }
208                 }
209
210                 bool IRepeatInfoUser.HasHeader {
211                         get { return false; }
212                 }
213
214                 bool IRepeatInfoUser.HasSeparators {
215                         get { return false; }
216                 }
217
218                 int IRepeatInfoUser.RepeatedItemCount {
219                         get { return Items.Count; }
220                 }
221
222                 protected override Style CreateControlStyle ()
223                 {
224                         return new TableStyle (ViewState);
225                 }
226
227 #if NET_2_0
228                 [MonoTODO]
229                 protected override Control FindControl (string id, int pathOffset)
230                 {
231                         throw new NotImplementedException ();
232                 }
233 #endif
234
235 #if NET_2_0
236                 protected virtual
237 #endif
238                 Style GetItemStyle (ListItemType itemType, int repeatIndex)
239                 {
240                         return null;
241                 }
242
243 #if NET_2_0
244                 protected virtual
245 #endif
246                 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
247                 {
248                         RadioButton radio = new RadioButton ();
249                         radio.Text = Items [repeatIndex].Text;
250                         radio.ID = ClientID + "_"  + repeatIndex;
251                         radio.TextAlign = TextAlign;
252                         radio.GroupName = UniqueID;
253                         radio.Page = Page;
254                         radio.Checked = Items [repeatIndex].Selected;
255                         radio.Attributes["Value"] = Items [repeatIndex].Value;
256                         radio.RenderControl (writer);
257                 }
258 #if NET_2_0
259                 protected virtual
260 #endif
261                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
262                 {
263                         string val = postCollection [postDataKey];
264                         ListItemCollection items = Items;
265                         int end = items.Count;
266                         int selected = SelectedIndex;
267                         for (int i = 0; i < end; i++) {
268                                 ListItem item = items [i];
269                                 if (item == null || val != item.Value)
270                                         continue;
271
272                                 if (i != selected) {
273                                         SelectedIndex = i;
274                                         need_raise = true;
275                                 }
276                                 return true;
277                         }
278
279                         return false;
280                 }
281
282 #if NET_2_0
283                 protected virtual
284 #endif
285                 void RaisePostDataChangedEvent ()
286                 {
287                         if (need_raise)
288                                 OnSelectedIndexChanged (EventArgs.Empty);
289                 }
290
291                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
292                 {
293                         return LoadPostData (postDataKey, postCollection);
294                 }
295                 
296                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
297                 {
298                         RaisePostDataChangedEvent ();
299                 }
300
301                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,  int repeatIndex)
302                 {
303                         return GetItemStyle (itemType, repeatIndex);
304                 }
305
306                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
307                 {
308                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
309                 }
310
311 #if NET_2_0
312                 protected internal
313 #else           
314                 protected
315 #endif          
316                 override void Render (HtmlTextWriter writer)
317                 {
318                         RepeatInfo repeat = new RepeatInfo ();
319                         repeat.RepeatColumns = RepeatColumns;
320                         repeat.RepeatDirection = RepeatDirection;
321                         repeat.RepeatLayout = RepeatLayout;
322                         repeat.RenderRepeater (writer, this, ControlStyle, this);
323                 }
324         }
325
326 }
327