2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web / System.Web.UI.WebControls / RadioButtonList.cs
blobb8ee5afaf284190c264f1d28db5df74f5f38a5f6
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 //
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Collections.Specialized;
35 using System.Security.Permissions;
37 namespace System.Web.UI.WebControls {
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;
54 public RadioButtonList ()
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
70 return ((TableStyle) ControlStyle).CellPadding;
73 set {
74 ((TableStyle) ControlStyle).CellPadding = value;
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
89 return ((TableStyle) ControlStyle).CellSpacing;
92 set {
93 ((TableStyle) ControlStyle).CellSpacing = value;
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);
108 set {
109 if (value < 0)
110 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
112 ViewState ["RepeatColumns"] = value;
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);
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.");
131 ViewState ["RepeatDirection"] = value;
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);
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.");
150 ViewState ["RepeatLayout"] = value;
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);
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.");
169 ViewState ["TextAlign"] = value;
173 // Interface properties
175 #if NET_2_0
176 protected virtual
177 #endif
178 bool HasFooter {
179 get { return false; }
182 #if NET_2_0
183 protected virtual
184 #endif
185 bool HasHeader {
186 get { return false; }
189 #if NET_2_0
190 protected virtual
191 #endif
192 bool HasSeparators {
193 get { return false; }
196 #if NET_2_0
197 protected virtual
198 #endif
199 int RepeatedItemCount {
200 get { return Items.Count; }
203 bool IRepeatInfoUser.HasFooter {
204 get { return HasFooter; }
207 bool IRepeatInfoUser.HasHeader {
208 get { return HasHeader; }
211 bool IRepeatInfoUser.HasSeparators {
212 get { return HasSeparators; }
215 int IRepeatInfoUser.RepeatedItemCount {
216 get { return RepeatedItemCount; }
219 protected override Style CreateControlStyle ()
221 return new TableStyle (ViewState);
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)
230 return this;
232 #endif
234 #if NET_2_0
235 protected virtual
236 #endif
237 Style GetItemStyle (ListItemType itemType, int repeatIndex)
239 return null;
242 #if NET_2_0
243 protected virtual
244 #endif
245 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
247 ListItem item = Items [repeatIndex];
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);
270 #if NET_2_0
271 protected virtual
272 #endif
273 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
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;
287 if (i != selected) {
288 SelectedIndex = i;
289 #if NET_2_0
290 return true;
291 #else
292 need_raise = true;
293 #endif
295 #if !NET_2_0
296 return true;
297 #endif
300 return false;
303 #if NET_2_0
304 protected virtual
305 #endif
306 void RaisePostDataChangedEvent ()
308 #if NET_2_0
309 ValidateEvent (UniqueID, String.Empty);
310 if (CausesValidation)
311 Page.Validate (ValidationGroup);
312 #endif
314 #if !NET_2_0
315 if (need_raise)
316 #endif
317 OnSelectedIndexChanged (EventArgs.Empty);
320 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
322 return LoadPostData (postDataKey, postCollection);
325 void IPostBackDataHandler.RaisePostDataChangedEvent ()
327 RaisePostDataChangedEvent ();
330 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
332 return GetItemStyle (itemType, repeatIndex);
335 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
337 RenderItem (itemType, repeatIndex, repeatInfo, writer);
340 #if NET_2_0
341 protected internal
342 #else
343 protected
344 #endif
345 override void Render (HtmlTextWriter writer)
347 #if NET_2_0
348 if (Page != null)
349 Page.ClientScript.RegisterForEventValidation (UniqueID);
351 if (Items.Count == 0)
352 return;
353 #endif
354 RepeatInfo repeat = new RepeatInfo ();
355 repeat.RepeatColumns = RepeatColumns;
356 repeat.RepeatDirection = RepeatDirection;
357 repeat.RepeatLayout = RepeatLayout;
359 tabIndex = TabIndex;
360 TabIndex = 0;
362 repeat.RenderRepeater (writer, this, ControlStyle, this);
364 TabIndex = tabIndex;