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
53 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
54 [WebSysDescription ("The border left within a RadioButton.")]
\r
55 public virtual int CellPadding
\r
59 if(ControlStyleCreated)
\r
61 return (int)(((TableStyle)ControlStyle).CellPadding);
\r
67 throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
\r
68 ((TableStyle)ControlStyle).CellPadding = value;
\r
72 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
73 [WebSysDescription ("The border left between RadioButtons.")]
\r
74 public virtual int CellSpacing
\r
78 if(ControlStyleCreated)
\r
80 return (int)(((TableStyle)ControlStyle).CellSpacing);
\r
86 throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
\r
87 ((TableStyle)ControlStyle).CellSpacing = value;
\r
91 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
92 [WebSysDescription ("The number of columns that should be used to display the RadioButtons.")]
\r
93 public virtual int RepeatColumns
\r
97 object o = ViewState["RepeatColumns"];
\r
104 throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
\r
105 ViewState["RepeatColumns"] = value;
\r
109 [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
110 [WebSysDescription ("The direction that is followed when doing the layout.")]
\r
111 public virtual RepeatDirection RepeatDirection
\r
115 object o = ViewState["RepeatDirection"];
\r
117 return (RepeatDirection)o;
\r
118 return RepeatDirection.Vertical;
\r
122 if(!Enum.IsDefined(typeof(RepeatDirection), value))
\r
123 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
124 ViewState["RepeatDirection"] = value;
\r
128 [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
129 [WebSysDescription ("The method used to create the layout.")]
\r
130 public virtual RepeatLayout RepeatLayout
\r
134 object o = ViewState["RepeatLayout"];
\r
136 return (RepeatLayout)o;
\r
137 return RepeatLayout.Table;
\r
141 if(!Enum.IsDefined(typeof(RepeatLayout), value))
\r
142 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
143 ViewState["RepeatLayout"] = value;
\r
147 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
148 [WebSysDescription ("The alignment of the RadioButton text.")]
\r
149 public virtual TextAlign TextAlign
\r
153 object o = ViewState["TextAlign"];
\r
155 return (TextAlign)o;
\r
156 return TextAlign.Right;
\r
160 if(!Enum.IsDefined(typeof(TextAlign), value))
\r
161 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
162 ViewState["TextAlign"] = value;
\r
166 protected override Style CreateControlStyle()
\r
168 return new TableStyle(ViewState);
\r
171 protected override void Render(HtmlTextWriter writer)
\r
173 RepeatInfo info = new RepeatInfo();
\r
174 Style cStyle = (ControlStyleCreated ? ControlStyle : null);
\r
175 bool dirty = false;
\r
176 tabIndex = TabIndex;
\r
179 dirty = !ViewState.IsItemDirty("TabIndex");
\r
182 info.RepeatColumns = RepeatColumns;
\r
183 info.RepeatDirection = RepeatDirection;
\r
184 info.RepeatLayout = RepeatLayout;
\r
185 info.RenderRepeater(writer, this, cStyle, this);
\r
188 TabIndex = tabIndex;
\r
192 ViewState.SetItemDirty("TabIndex", false);
\r
196 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
198 string value = postCollection [postDataKey];
200 for (int i = 0; i < c; i++) {
201 if (Items [i].Value != value)
204 if (i != SelectedIndex) {
206 selectionIndexChanged = true;
215 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
217 if(selectionIndexChanged)
\r
218 OnSelectedIndexChanged(EventArgs.Empty);
\r
221 Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)
\r
226 void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,
\r
228 RepeatInfo repeatInfo,
\r
229 HtmlTextWriter writer)
\r
231 /* Create a new RadioButton as if it was defined in the page and render it */
\r
232 RadioButton button = new RadioButton ();
\r
234 button.GroupName = UniqueID;
\r
235 button.TextAlign = TextAlign;
\r
236 button.AutoPostBack = AutoPostBack;
\r
237 button.ID = ClientID + "_" + repeatIndex.ToString (NumberFormatInfo.InvariantInfo);;
\r
238 button.TabIndex = tabIndex;
239 ListItem current = Items [repeatIndex];
\r
240 button.Text = current.Text;
\r
241 button.Attributes ["value"] = current.Value;
\r
242 button.Checked = current.Selected;
\r
243 button.Enabled = Enabled;
\r
244 button.RenderControl (writer);
\r
247 bool IRepeatInfoUser.HasFooter
\r
255 bool IRepeatInfoUser.HasHeader
\r
263 bool IRepeatInfoUser.HasSeparators
\r
271 int IRepeatInfoUser.RepeatedItemCount
\r
275 return Items.Count;
\r