2007-07-24 Igor Zelmanovich <igorz@mainsoft.com>
[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                 protected virtual
177 #endif
178                 bool HasFooter {
179                         get { return false; }
180                 }
181
182 #if NET_2_0
183                 protected virtual
184 #endif
185                 bool HasHeader {
186                         get { return false; }
187                 }
188
189 #if NET_2_0
190                 protected virtual
191 #endif
192                 bool HasSeparators {
193                         get { return false; }
194                 }
195
196 #if NET_2_0
197                 protected virtual
198 #endif
199                 int RepeatedItemCount {
200                         get { return Items.Count; }
201                 }
202                 
203                 bool IRepeatInfoUser.HasFooter {
204                         get { return HasFooter; }
205                 }
206
207                 bool IRepeatInfoUser.HasHeader {
208                         get { return HasHeader; }
209                 }
210
211                 bool IRepeatInfoUser.HasSeparators {
212                         get { return HasSeparators; }
213                 }
214
215                 int IRepeatInfoUser.RepeatedItemCount {
216                         get { return RepeatedItemCount; }
217                 }
218
219                 protected override Style CreateControlStyle ()
220                 {
221                         return new TableStyle (ViewState);
222                 }
223
224 #if NET_2_0
225                 // MSDN: Searches the current naming container for a server control 
226                 // with the specified ID and path offset. The FindControl method 
227                 // always returns the RadioButtonList object. 
228                 protected override Control FindControl (string id, int pathOffset)
229                 {
230                         return this;
231                 }
232 #endif
233
234 #if NET_2_0
235                 protected virtual
236 #endif
237                 Style GetItemStyle (ListItemType itemType, int repeatIndex)
238                 {
239                         return null;
240                 }
241
242 #if NET_2_0
243                 protected virtual
244 #endif
245                 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
246                 {
247                         ListItem item = Items [repeatIndex];
248
249                         RadioButton radio = new RadioButton ();
250                         radio.Text = item.Text;
251                         radio.ID = ClientID + "_"  + repeatIndex;
252                         radio.TextAlign = TextAlign;
253                         radio.GroupName = UniqueID;
254                         radio.Page = Page;
255                         radio.Checked = item.Selected;
256                         radio.ValueAttribute = item.Value;
257                         radio.AutoPostBack = AutoPostBack;
258                         radio.Enabled = Enabled;
259                         radio.TabIndex = tabIndex;
260 #if NET_2_0
261                         radio.ValidationGroup = ValidationGroup;
262                         radio.CausesValidation = CausesValidation;
263                         if (radio.HasAttributes)
264                                 radio.Attributes.Clear ();
265                         if (item.HasAttributes)
266                                 radio.Attributes.CopyFrom (item.Attributes);
267 #endif
268                         radio.RenderControl (writer);
269                 }
270 #if NET_2_0
271                 protected virtual
272 #endif
273                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
274                 {
275 #if NET_2_0
276                         EnsureDataBound ();
277 #endif
278                         string val = postCollection [postDataKey];
279                         ListItemCollection items = Items;
280                         int end = items.Count;
281                         int selected = SelectedIndex;
282                         for (int i = 0; i < end; i++) {
283                                 ListItem item = items [i];
284                                 if (item == null || val != item.Value)
285                                         continue;
286
287                                 if (i != selected) {
288                                         SelectedIndex = i;
289 #if NET_2_0
290                                         return true;
291 #else
292                                         need_raise = true;
293 #endif
294                                 }
295 #if !NET_2_0
296                                 return true;
297 #endif
298                         }
299
300                         return false;
301                 }
302
303 #if NET_2_0
304                 protected virtual
305 #endif
306                 void RaisePostDataChangedEvent ()
307                 {
308 #if NET_2_0
309                         if (CausesValidation)
310                                 Page.Validate (ValidationGroup);
311 #endif
312
313 #if !NET_2_0
314                         if (need_raise)
315 #endif
316                                 OnSelectedIndexChanged (EventArgs.Empty);
317                 }
318
319                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
320                 {
321                         return LoadPostData (postDataKey, postCollection);
322                 }
323                 
324                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
325                 {
326                         RaisePostDataChangedEvent ();
327                 }
328
329                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,  int repeatIndex)
330                 {
331                         return GetItemStyle (itemType, repeatIndex);
332                 }
333
334                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
335                 {
336                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
337                 }
338
339 #if NET_2_0
340                 protected internal
341 #else           
342                 protected
343 #endif          
344                 override void Render (HtmlTextWriter writer)
345                 {
346 #if NET_2_0
347                         if (Page != null)
348                                 Page.ClientScript.RegisterForEventValidation (this.UniqueID);
349
350                         if (Items.Count == 0)
351                                 return;
352 #endif
353                         RepeatInfo repeat = new RepeatInfo ();
354                         repeat.RepeatColumns = RepeatColumns;
355                         repeat.RepeatDirection = RepeatDirection;
356                         repeat.RepeatLayout = RepeatLayout;
357
358                         tabIndex = TabIndex;
359                         TabIndex = 0;
360
361                         repeat.RenderRepeater (writer, this, ControlStyle, this);
362
363                         TabIndex = tabIndex;
364                 }
365         }
366
367 }
368
369
370
371
372