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")]
44 [SupportsEventValidation]
45 public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
49 public RadioButtonList ()
55 [WebSysDescription ("")]
56 [WebCategory ("Layout")]
57 public virtual int CellPadding {
59 if (ControlStyleCreated == false)
60 return -1; // default value
62 return ((TableStyle) ControlStyle).CellPadding;
66 ((TableStyle) ControlStyle).CellPadding = value;
71 [WebSysDescription ("")]
72 [WebCategory ("Layout")]
73 public virtual int CellSpacing {
75 if (ControlStyleCreated == false)
76 return -1; // default value
78 return ((TableStyle) ControlStyle).CellSpacing;
82 ((TableStyle) ControlStyle).CellSpacing = value;
87 [WebSysDescription ("")]
88 [WebCategory ("Layout")]
89 public virtual int RepeatColumns {
91 return ViewState.GetInt ("RepeatColumns", 0);
96 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
98 ViewState ["RepeatColumns"] = value;
102 [DefaultValue (RepeatDirection.Vertical)]
103 [WebSysDescription ("")]
104 [WebCategory ("Layout")]
105 public virtual RepeatDirection RepeatDirection {
107 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
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.");
114 ViewState ["RepeatDirection"] = value;
118 [DefaultValue (RepeatLayout.Table)]
119 [WebSysDescription ("")]
120 [WebCategory ("Layout")]
121 public virtual RepeatLayout RepeatLayout {
123 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
128 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.OrderedList;
130 throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
132 ViewState ["RepeatLayout"] = value;
136 [DefaultValue (TextAlign.Right)]
137 [WebSysDescription ("")]
138 [WebCategory ("Appearance")]
139 public virtual TextAlign TextAlign {
141 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
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.");
148 ViewState ["TextAlign"] = value;
152 // Interface properties
154 protected virtual bool HasFooter {
155 get { return false; }
158 protected virtual bool HasHeader {
159 get { return false; }
162 protected virtual bool HasSeparators {
163 get { return false; }
166 protected virtual int RepeatedItemCount {
167 get { return Items.Count; }
170 bool IRepeatInfoUser.HasFooter {
171 get { return HasFooter; }
174 bool IRepeatInfoUser.HasHeader {
175 get { return HasHeader; }
178 bool IRepeatInfoUser.HasSeparators {
179 get { return HasSeparators; }
182 int IRepeatInfoUser.RepeatedItemCount {
183 get { return RepeatedItemCount; }
186 protected override Style CreateControlStyle ()
188 return new TableStyle (ViewState);
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)
199 protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
204 protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
206 ListItem item = Items [repeatIndex];
208 RadioButton radio = new RadioButton ();
209 radio.Text = item.Text;
210 radio.ID = ClientID + "_" + repeatIndex;
211 radio.TextAlign = TextAlign;
212 radio.GroupName = UniqueID;
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);
226 radio.RenderControl (writer);
229 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
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)
250 protected virtual void RaisePostDataChangedEvent ()
252 ValidateEvent (UniqueID, String.Empty);
254 if (CausesValidation && page != null)
255 page.Validate (ValidationGroup);
257 OnSelectedIndexChanged (EventArgs.Empty);
260 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
262 return LoadPostData (postDataKey, postCollection);
265 void IPostBackDataHandler.RaisePostDataChangedEvent ()
267 RaisePostDataChangedEvent ();
270 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
272 return GetItemStyle (itemType, repeatIndex);
275 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
277 RenderItem (itemType, repeatIndex, repeatInfo, writer);
280 protected internal override void Render (HtmlTextWriter writer)
284 page.ClientScript.RegisterForEventValidation (UniqueID);
286 if (Items.Count == 0)
289 RepeatInfo repeat = new RepeatInfo ();
290 repeat.RepeatColumns = RepeatColumns;
291 repeat.RepeatDirection = RepeatDirection;
292 repeat.RepeatLayout = RepeatLayout;
297 repeat.RenderRepeater (writer, this, ControlStyle, this);