2 // System.Web.UI.WebControls.RadioButtonList.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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 {
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [ValidationProperty ("SelectedItem")]
45 [SupportsEventValidation]
47 public class RadioButtonList : ListControl, IRepeatInfoUser,
48 INamingContainer, IPostBackDataHandler {
54 public RadioButtonList ()
63 [WebSysDescription ("")]
64 [WebCategory ("Layout")]
65 public virtual int CellPadding {
67 if (ControlStyleCreated == false)
68 return -1; // default value
70 return ((TableStyle) ControlStyle).CellPadding;
74 ((TableStyle) ControlStyle).CellPadding = value;
82 [WebSysDescription ("")]
83 [WebCategory ("Layout")]
84 public virtual int CellSpacing {
86 if (ControlStyleCreated == false)
87 return -1; // default value
89 return ((TableStyle) ControlStyle).CellSpacing;
93 ((TableStyle) ControlStyle).CellSpacing = value;
101 [WebSysDescription ("")]
102 [WebCategory ("Layout")]
103 public virtual int RepeatColumns {
105 return ViewState.GetInt ("RepeatColumns", 0);
110 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
112 ViewState ["RepeatColumns"] = value;
119 [DefaultValue (RepeatDirection.Vertical)]
120 [WebSysDescription ("")]
121 [WebCategory ("Layout")]
122 public virtual RepeatDirection RepeatDirection {
124 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
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;
138 [DefaultValue (RepeatLayout.Table)]
139 [WebSysDescription ("")]
140 [WebCategory ("Layout")]
141 public virtual RepeatLayout RepeatLayout {
143 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
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;
157 [DefaultValue (TextAlign.Right)]
158 [WebSysDescription ("")]
159 [WebCategory ("Appearance")]
160 public virtual TextAlign TextAlign {
162 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
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
177 protected virtual bool HasFooter
180 throw new NotImplementedException ();
185 protected virtual bool HasHeader
188 throw new NotImplementedException ();
193 protected virtual bool HasSeparators
196 throw new NotImplementedException ();
201 protected virtual int RepeatedItemCount
204 throw new NotImplementedException ();
209 bool IRepeatInfoUser.HasFooter {
210 get { return false; }
213 bool IRepeatInfoUser.HasHeader {
214 get { return false; }
217 bool IRepeatInfoUser.HasSeparators {
218 get { return false; }
221 int IRepeatInfoUser.RepeatedItemCount {
222 get { return Items.Count; }
225 protected override Style CreateControlStyle ()
227 return new TableStyle (ViewState);
232 protected override Control FindControl (string id, int pathOffset)
234 throw new NotImplementedException ();
241 Style GetItemStyle (ListItemType itemType, int repeatIndex)
249 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
251 RadioButton radio = new RadioButton ();
252 radio.Text = Items [repeatIndex].Text;
253 radio.ID = ClientID + "_" + repeatIndex;
254 radio.TextAlign = TextAlign;
255 radio.GroupName = UniqueID;
257 radio.Checked = Items [repeatIndex].Selected;
258 radio.ValueAttribute = Items [repeatIndex].Value;
259 radio.AutoPostBack = AutoPostBack;
260 radio.Enabled = Enabled;
261 radio.TabIndex = tabIndex;
263 radio.ValidationGroup = ValidationGroup;
264 radio.CausesValidation = CausesValidation;
266 radio.RenderControl (writer);
271 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
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)
301 void RaisePostDataChangedEvent ()
304 if (CausesValidation)
305 Page.Validate (ValidationGroup);
311 OnSelectedIndexChanged (EventArgs.Empty);
314 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
316 return LoadPostData (postDataKey, postCollection);
319 void IPostBackDataHandler.RaisePostDataChangedEvent ()
321 RaisePostDataChangedEvent ();
324 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
326 return GetItemStyle (itemType, repeatIndex);
329 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
331 RenderItem (itemType, repeatIndex, repeatInfo, writer);
339 override void Render (HtmlTextWriter writer)
341 RepeatInfo repeat = new RepeatInfo ();
342 repeat.RepeatColumns = RepeatColumns;
343 repeat.RepeatDirection = RepeatDirection;
344 repeat.RepeatLayout = RepeatLayout;
349 repeat.RenderRepeater (writer, this, ControlStyle, this);