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;
36 namespace System.Web.UI.WebControls {
38 [ValidationProperty ("SelectedItem")]
39 public class RadioButtonList : ListControl, IRepeatInfoUser,
40 INamingContainer, IPostBackDataHandler {
43 public RadioButtonList ()
52 [WebSysDescription ("")]
53 [WebCategory ("Layout")]
54 public virtual int CellPadding {
56 if (ControlStyleCreated == false)
57 return -1; // default value
59 return ((TableStyle) ControlStyle).CellPadding;
63 ((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;
90 [WebSysDescription ("")]
91 [WebCategory ("Layout")]
92 public virtual int RepeatColumns {
94 return ViewState.GetInt ("RepeatColumns", 0);
99 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
101 ViewState ["RepeatColumns"] = value;
108 [DefaultValue (RepeatDirection.Vertical)]
109 [WebSysDescription ("")]
110 [WebCategory ("Layout")]
111 public virtual RepeatDirection RepeatDirection {
113 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
117 if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
118 throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
120 ViewState ["RepeatDirection"] = value;
127 [DefaultValue (RepeatLayout.Table)]
128 [WebSysDescription ("")]
129 [WebCategory ("Layout")]
130 public virtual RepeatLayout RepeatLayout {
132 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
136 if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
137 throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
139 ViewState ["RepeatLayout"] = value;
146 [DefaultValue (TextAlign.Right)]
147 [WebSysDescription ("")]
148 [WebCategory ("Appearance")]
149 public virtual TextAlign TextAlign {
151 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
155 if (value != TextAlign.Left && value != TextAlign.Right)
156 throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
158 ViewState ["TextAlign"] = value;
162 // Interface properties
166 protected virtual bool HasFooter
169 throw new NotImplementedException ();
174 protected virtual bool HasHeader
177 throw new NotImplementedException ();
182 protected virtual bool HasSeparators
185 throw new NotImplementedException ();
190 protected virtual int RepeatedItemCount
193 throw new NotImplementedException ();
198 bool IRepeatInfoUser.HasFooter {
199 get { return false; }
202 bool IRepeatInfoUser.HasHeader {
203 get { return false; }
206 bool IRepeatInfoUser.HasSeparators {
207 get { return false; }
210 int IRepeatInfoUser.RepeatedItemCount {
211 get { return Items.Count; }
214 protected override Style CreateControlStyle ()
216 return new TableStyle (ViewState);
221 protected override Control FindControl (string id, int pathOffset)
223 throw new NotImplementedException ();
230 Style GetItemStyle (ListItemType itemType, int repeatIndex)
238 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
240 RadioButton radio = new RadioButton ();
241 radio.Text = Items [repeatIndex].Text;
242 radio.ID = ClientID + "_" + repeatIndex;
243 radio.TextAlign = TextAlign;
244 radio.GroupName = UniqueID;
246 radio.Checked = Items [repeatIndex].Selected;
247 radio.Attributes["Value"] = Items [repeatIndex].Value;
248 radio.RenderControl (writer);
253 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
255 string val = postCollection [postDataKey];
256 ListItemCollection items = Items;
257 int end = items.Count;
258 int selected = SelectedIndex;
259 for (int i = 0; i < end; i++) {
260 ListItem item = items [i];
261 if (item == null || val != item.Value)
277 void RaisePostDataChangedEvent ()
280 OnSelectedIndexChanged (EventArgs.Empty);
283 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
285 return LoadPostData (postDataKey, postCollection);
288 void IPostBackDataHandler.RaisePostDataChangedEvent ()
290 RaisePostDataChangedEvent ();
293 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
295 return GetItemStyle (itemType, repeatIndex);
298 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
300 RenderItem (itemType, repeatIndex, repeatInfo, writer);
308 override void Render (HtmlTextWriter writer)
310 RepeatInfo repeat = new RepeatInfo ();
311 repeat.RepeatColumns = RepeatColumns;
312 repeat.RepeatDirection = RepeatDirection;
313 repeat.RepeatLayout = RepeatLayout;
314 repeat.RenderRepeater (writer, this, ControlStyle, this);