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 {
51 public RadioButtonList ()
60 [WebSysDescription ("")]
61 [WebCategory ("Layout")]
62 public virtual int CellPadding {
64 if (ControlStyleCreated == false)
65 return -1; // default value
67 return ((TableStyle) ControlStyle).CellPadding;
71 ((TableStyle) ControlStyle).CellPadding = value;
79 [WebSysDescription ("")]
80 [WebCategory ("Layout")]
81 public virtual int CellSpacing {
83 if (ControlStyleCreated == false)
84 return -1; // default value
86 return ((TableStyle) ControlStyle).CellSpacing;
90 ((TableStyle) ControlStyle).CellSpacing = value;
98 [WebSysDescription ("")]
99 [WebCategory ("Layout")]
100 public virtual int RepeatColumns {
102 return ViewState.GetInt ("RepeatColumns", 0);
107 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
109 ViewState ["RepeatColumns"] = value;
116 [DefaultValue (RepeatDirection.Vertical)]
117 [WebSysDescription ("")]
118 [WebCategory ("Layout")]
119 public virtual RepeatDirection RepeatDirection {
121 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
125 if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
126 throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
128 ViewState ["RepeatDirection"] = value;
135 [DefaultValue (RepeatLayout.Table)]
136 [WebSysDescription ("")]
137 [WebCategory ("Layout")]
138 public virtual RepeatLayout RepeatLayout {
140 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
144 if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
145 throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
147 ViewState ["RepeatLayout"] = value;
154 [DefaultValue (TextAlign.Right)]
155 [WebSysDescription ("")]
156 [WebCategory ("Appearance")]
157 public virtual TextAlign TextAlign {
159 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
163 if (value != TextAlign.Left && value != TextAlign.Right)
164 throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
166 ViewState ["TextAlign"] = value;
170 // Interface properties
174 protected virtual bool HasFooter
177 throw new NotImplementedException ();
182 protected virtual bool HasHeader
185 throw new NotImplementedException ();
190 protected virtual bool HasSeparators
193 throw new NotImplementedException ();
198 protected virtual int RepeatedItemCount
201 throw new NotImplementedException ();
206 bool IRepeatInfoUser.HasFooter {
207 get { return false; }
210 bool IRepeatInfoUser.HasHeader {
211 get { return false; }
214 bool IRepeatInfoUser.HasSeparators {
215 get { return false; }
218 int IRepeatInfoUser.RepeatedItemCount {
219 get { return Items.Count; }
222 protected override Style CreateControlStyle ()
224 return new TableStyle (ViewState);
229 protected override Control FindControl (string id, int pathOffset)
231 throw new NotImplementedException ();
238 Style GetItemStyle (ListItemType itemType, int repeatIndex)
246 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
248 RadioButton radio = new RadioButton ();
249 radio.Text = Items [repeatIndex].Text;
250 radio.ID = ClientID + "_" + repeatIndex;
251 radio.TextAlign = TextAlign;
252 radio.GroupName = UniqueID;
254 radio.Checked = Items [repeatIndex].Selected;
255 radio.Attributes["Value"] = Items [repeatIndex].Value;
256 radio.RenderControl (writer);
261 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
263 string val = postCollection [postDataKey];
264 ListItemCollection items = Items;
265 int end = items.Count;
266 int selected = SelectedIndex;
267 for (int i = 0; i < end; i++) {
268 ListItem item = items [i];
269 if (item == null || val != item.Value)
285 void RaisePostDataChangedEvent ()
288 OnSelectedIndexChanged (EventArgs.Empty);
291 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
293 return LoadPostData (postDataKey, postCollection);
296 void IPostBackDataHandler.RaisePostDataChangedEvent ()
298 RaisePostDataChangedEvent ();
301 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
303 return GetItemStyle (itemType, repeatIndex);
306 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
308 RenderItem (itemType, repeatIndex, repeatInfo, writer);
316 override void Render (HtmlTextWriter writer)
318 RepeatInfo repeat = new RepeatInfo ();
319 repeat.RepeatColumns = RepeatColumns;
320 repeat.RepeatDirection = RepeatDirection;
321 repeat.RepeatLayout = RepeatLayout;
322 repeat.RenderRepeater (writer, this, ControlStyle, this);