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