2 // System.Web.UI.WebControls.RadioButtonList.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections.Specialized;
\r
35 using System.ComponentModel;
\r
36 using System.Globalization;
\r
38 using System.Web.UI;
\r
40 namespace System.Web.UI.WebControls
\r
42 [ValidationProperty("SelectedItem")]
\r
43 public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
\r
45 private bool selectionIndexChanged;
\r
46 private short tabIndex;
\r
48 public RadioButtonList(): base()
\r
50 selectionIndexChanged = false;
\r
56 [DefaultValue (-1), WebCategory ("Layout")]
57 [WebSysDescription ("The border left within a RadioButton.")]
\r
58 public virtual int CellPadding
\r
62 if(ControlStyleCreated)
\r
64 return (int)(((TableStyle)ControlStyle).CellPadding);
\r
70 throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
\r
71 ((TableStyle)ControlStyle).CellPadding = value;
\r
78 [DefaultValue (-1), WebCategory ("Layout")]
79 [WebSysDescription ("The border left between RadioButtons.")]
\r
80 public virtual int CellSpacing
\r
84 if(ControlStyleCreated)
\r
86 return (int)(((TableStyle)ControlStyle).CellSpacing);
\r
92 throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
\r
93 ((TableStyle)ControlStyle).CellSpacing = value;
\r
100 [DefaultValue (0), WebCategory ("Layout")]
101 [WebSysDescription ("The number of columns that should be used to display the RadioButtons.")]
\r
102 public virtual int RepeatColumns
\r
106 object o = ViewState["RepeatColumns"];
\r
113 throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
\r
114 ViewState["RepeatColumns"] = value;
\r
121 [DefaultValue (typeof (RepeatDirection), "Vertical"), WebCategory ("Layout")]
122 [WebSysDescription ("The direction that is followed when doing the layout.")]
\r
123 public virtual RepeatDirection RepeatDirection
\r
127 object o = ViewState["RepeatDirection"];
\r
129 return (RepeatDirection)o;
\r
130 return RepeatDirection.Vertical;
\r
134 if(!Enum.IsDefined(typeof(RepeatDirection), value))
\r
135 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
136 ViewState["RepeatDirection"] = value;
\r
143 [DefaultValue (typeof (RepeatLayout), "Table"), WebCategory ("Layout")]
144 [WebSysDescription ("The method used to create the layout.")]
\r
145 public virtual RepeatLayout RepeatLayout
\r
149 object o = ViewState["RepeatLayout"];
\r
151 return (RepeatLayout)o;
\r
152 return RepeatLayout.Table;
\r
156 if(!Enum.IsDefined(typeof(RepeatLayout), value))
\r
157 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
158 ViewState["RepeatLayout"] = value;
\r
165 [DefaultValue (typeof (TextAlign), "Right"), WebCategory ("Appearance")]
166 [WebSysDescription ("The alignment of the RadioButton text.")]
\r
167 public virtual TextAlign TextAlign
\r
171 object o = ViewState["TextAlign"];
\r
173 return (TextAlign)o;
\r
174 return TextAlign.Right;
\r
178 if(!Enum.IsDefined(typeof(TextAlign), value))
\r
179 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
180 ViewState["TextAlign"] = value;
\r
184 protected override Style CreateControlStyle()
\r
186 return new TableStyle(ViewState);
\r
189 protected override void Render(HtmlTextWriter writer)
\r
191 RepeatInfo info = new RepeatInfo();
\r
192 Style cStyle = (ControlStyleCreated ? ControlStyle : null);
\r
193 bool dirty = false;
\r
194 tabIndex = TabIndex;
\r
197 dirty = !ViewState.IsItemDirty("TabIndex");
\r
200 info.RepeatColumns = RepeatColumns;
\r
201 info.RepeatDirection = RepeatDirection;
\r
202 info.RepeatLayout = RepeatLayout;
\r
203 info.RenderRepeater(writer, this, cStyle, this);
\r
206 TabIndex = tabIndex;
\r
210 ViewState.SetItemDirty("TabIndex", false);
\r
214 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
216 string value = postCollection [postDataKey];
218 for (int i = 0; i < c; i++) {
219 if (Items [i].Value != value)
222 if (i != SelectedIndex) {
224 selectionIndexChanged = true;
233 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
235 if(selectionIndexChanged)
\r
236 OnSelectedIndexChanged(EventArgs.Empty);
\r
239 Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)
\r
244 void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,
\r
246 RepeatInfo repeatInfo,
\r
247 HtmlTextWriter writer)
\r
249 /* Create a new RadioButton as if it was defined in the page and render it */
\r
250 RadioButton button = new RadioButton ();
\r
252 button.GroupName = UniqueID;
\r
253 button.TextAlign = TextAlign;
\r
254 button.AutoPostBack = AutoPostBack;
\r
255 button.ID = ClientID + "_" + repeatIndex.ToString (NumberFormatInfo.InvariantInfo);;
\r
256 button.TabIndex = tabIndex;
257 ListItem current = Items [repeatIndex];
\r
258 button.Text = current.Text;
\r
259 button.Attributes ["value"] = current.Value;
\r
260 button.Checked = current.Selected;
\r
261 button.Enabled = Enabled;
\r
262 button.RenderControl (writer);
\r
265 bool IRepeatInfoUser.HasFooter
\r
273 bool IRepeatInfoUser.HasHeader
\r
281 bool IRepeatInfoUser.HasSeparators
\r
289 int IRepeatInfoUser.RepeatedItemCount
\r
293 return Items.Count;
\r