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
{
54 public RadioButtonList ()
63 [WebSysDescription ("")]
64 [WebCategory ("Layout")]
65 public virtual int CellPadding
{
67 if (ControlStyleCreated
== false)
68 return -1; // default value
70 return ((TableStyle
) ControlStyle
).CellPadding
;
74 ((TableStyle
) ControlStyle
).CellPadding
= value;
82 [WebSysDescription ("")]
83 [WebCategory ("Layout")]
84 public virtual int CellSpacing
{
86 if (ControlStyleCreated
== false)
87 return -1; // default value
89 return ((TableStyle
) ControlStyle
).CellSpacing
;
93 ((TableStyle
) ControlStyle
).CellSpacing
= value;
101 [WebSysDescription ("")]
102 [WebCategory ("Layout")]
103 public virtual int RepeatColumns
{
105 return ViewState
.GetInt ("RepeatColumns", 0);
110 throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
112 ViewState
["RepeatColumns"] = value;
119 [DefaultValue (RepeatDirection
.Vertical
)]
120 [WebSysDescription ("")]
121 [WebCategory ("Layout")]
122 public virtual RepeatDirection RepeatDirection
{
124 return (RepeatDirection
) ViewState
.GetInt ("RepeatDirection", (int) RepeatDirection
.Vertical
);
128 if (value != RepeatDirection
.Horizontal
&& value != RepeatDirection
.Vertical
)
129 throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
131 ViewState
["RepeatDirection"] = value;
138 [DefaultValue (RepeatLayout
.Table
)]
139 [WebSysDescription ("")]
140 [WebCategory ("Layout")]
141 public virtual RepeatLayout RepeatLayout
{
143 return (RepeatLayout
) ViewState
.GetInt ("RepeatLayout", (int) RepeatLayout
.Table
);
147 if (value != RepeatLayout
.Flow
&& value != RepeatLayout
.Table
)
148 throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
150 ViewState
["RepeatLayout"] = value;
157 [DefaultValue (TextAlign
.Right
)]
158 [WebSysDescription ("")]
159 [WebCategory ("Appearance")]
160 public virtual TextAlign TextAlign
{
162 return (TextAlign
)ViewState
.GetInt ("TextAlign", (int) TextAlign
.Right
);
166 if (value != TextAlign
.Left
&& value != TextAlign
.Right
)
167 throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
169 ViewState
["TextAlign"] = value;
173 // Interface properties
179 get { return false; }
186 get { return false; }
193 get { return false; }
199 int RepeatedItemCount
{
200 get { return Items.Count; }
203 bool IRepeatInfoUser
.HasFooter
{
204 get { return HasFooter; }
207 bool IRepeatInfoUser
.HasHeader
{
208 get { return HasHeader; }
211 bool IRepeatInfoUser
.HasSeparators
{
212 get { return HasSeparators; }
215 int IRepeatInfoUser
.RepeatedItemCount
{
216 get { return RepeatedItemCount; }
219 protected override Style
CreateControlStyle ()
221 return new TableStyle (ViewState
);
225 // MSDN: Searches the current naming container for a server control
226 // with the specified ID and path offset. The FindControl method
227 // always returns the RadioButtonList object.
228 protected override Control
FindControl (string id
, int pathOffset
)
237 Style
GetItemStyle (ListItemType itemType
, int repeatIndex
)
245 void RenderItem (ListItemType itemType
, int repeatIndex
, RepeatInfo repeatInfo
, HtmlTextWriter writer
)
247 ListItem item
= Items
[repeatIndex
];
249 RadioButton radio
= new RadioButton ();
250 radio
.Text
= item
.Text
;
251 radio
.ID
= ClientID
+ "_" + repeatIndex
;
252 radio
.TextAlign
= TextAlign
;
253 radio
.GroupName
= UniqueID
;
255 radio
.Checked
= item
.Selected
;
256 radio
.ValueAttribute
= item
.Value
;
257 radio
.AutoPostBack
= AutoPostBack
;
258 radio
.Enabled
= Enabled
;
259 radio
.TabIndex
= tabIndex
;
261 radio
.ValidationGroup
= ValidationGroup
;
262 radio
.CausesValidation
= CausesValidation
;
263 if (radio
.HasAttributes
)
264 radio
.Attributes
.Clear ();
265 if (item
.HasAttributes
)
266 radio
.Attributes
.CopyFrom (item
.Attributes
);
268 radio
.RenderControl (writer
);
273 bool LoadPostData (string postDataKey
, NameValueCollection postCollection
)
278 string val
= postCollection
[postDataKey
];
279 ListItemCollection items
= Items
;
280 int end
= items
.Count
;
281 int selected
= SelectedIndex
;
282 for (int i
= 0; i
< end
; i
++) {
283 ListItem item
= items
[i
];
284 if (item
== null || val
!= item
.Value
)
306 void RaisePostDataChangedEvent ()
309 ValidateEvent (UniqueID
, String
.Empty
);
310 if (CausesValidation
)
311 Page
.Validate (ValidationGroup
);
317 OnSelectedIndexChanged (EventArgs
.Empty
);
320 bool IPostBackDataHandler
.LoadPostData (string postDataKey
, NameValueCollection postCollection
)
322 return LoadPostData (postDataKey
, postCollection
);
325 void IPostBackDataHandler
.RaisePostDataChangedEvent ()
327 RaisePostDataChangedEvent ();
330 Style IRepeatInfoUser
.GetItemStyle (ListItemType itemType
, int repeatIndex
)
332 return GetItemStyle (itemType
, repeatIndex
);
335 void IRepeatInfoUser
.RenderItem (ListItemType itemType
, int repeatIndex
, RepeatInfo repeatInfo
, HtmlTextWriter writer
)
337 RenderItem (itemType
, repeatIndex
, repeatInfo
, writer
);
345 override void Render (HtmlTextWriter writer
)
349 Page
.ClientScript
.RegisterForEventValidation (UniqueID
);
351 if (Items
.Count
== 0)
354 RepeatInfo repeat
= new RepeatInfo ();
355 repeat
.RepeatColumns
= RepeatColumns
;
356 repeat
.RepeatDirection
= RepeatDirection
;
357 repeat
.RepeatLayout
= RepeatLayout
;
362 repeat
.RenderRepeater (writer
, this, ControlStyle
, this);