2005-02-02 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / RadioButtonList.cs
1 //
2 // System.Web.UI.WebControls.RadioButtonList.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32 \r
33 using System;\r
34 using System.Collections.Specialized;\r
35 using System.ComponentModel;\r
36 using System.Globalization;\r
37 using System.Web;\r
38 using System.Web.UI;\r
39 \r
40 namespace System.Web.UI.WebControls\r
41 {\r
42         [ValidationProperty("SelectedItem")]\r
43         public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler\r
44         {\r
45                 private bool  selectionIndexChanged;\r
46                 private short  tabIndex;\r
47 \r
48                 public RadioButtonList(): base()\r
49                 {\r
50                         selectionIndexChanged = false;\r
51                 }\r
52
53 #if !NET_2_0\r
54                 [Bindable (true)]\r
55 #endif\r
56                 [DefaultValue (-1), WebCategory ("Layout")]
57                 [WebSysDescription ("The border left within a RadioButton.")]\r
58                 public virtual int CellPadding\r
59                 {\r
60                         get\r
61                         {\r
62                                 if(ControlStyleCreated)\r
63                                 {\r
64                                         return (int)(((TableStyle)ControlStyle).CellPadding);\r
65                                 }\r
66                                 return -1;\r
67                         }\r
68                         set {
69                                 if (value < -1)
70                                         throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");\r
71                                 ((TableStyle)ControlStyle).CellPadding = value;\r
72                         }\r
73                 }\r
74
75 #if !NET_2_0\r
76                 [Bindable (true)]\r
77 #endif\r
78                 [DefaultValue (-1), WebCategory ("Layout")]
79                 [WebSysDescription ("The border left between RadioButtons.")]\r
80                 public virtual int CellSpacing\r
81                 {\r
82                         get\r
83                         {\r
84                                 if(ControlStyleCreated)\r
85                                 {\r
86                                         return (int)(((TableStyle)ControlStyle).CellSpacing);\r
87                                 }\r
88                                 return -1;\r
89                         }\r
90                         set {
91                                 if (value < -1)
92                                         throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");\r
93                                 ((TableStyle)ControlStyle).CellSpacing = value;\r
94                         }\r
95                 }\r
96
97 #if !NET_2_0\r
98                 [Bindable (true)]\r
99 #endif\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
103                 {\r
104                         get\r
105                         {\r
106                                 object o = ViewState["RepeatColumns"];\r
107                                 if(o != null)\r
108                                         return (int)o;\r
109                                 return 0;\r
110                         }\r
111                         set {\r
112                                 if (value < 0)\r
113                                         throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");\r
114                                 ViewState["RepeatColumns"] = value;\r
115                         }\r
116                 }\r
117
118 #if !NET_2_0\r
119                 [Bindable (true)]\r
120 #endif\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
124                 {\r
125                         get\r
126                         {\r
127                                 object o = ViewState["RepeatDirection"];\r
128                                 if(o != null)\r
129                                         return (RepeatDirection)o;\r
130                                 return RepeatDirection.Vertical;\r
131                         }\r
132                         set\r
133                         {\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
137                         }\r
138                 }\r
139
140 #if !NET_2_0\r
141                 [Bindable (true)]\r
142 #endif\r
143                 [DefaultValue (typeof (RepeatLayout), "Table"), WebCategory ("Layout")]
144                 [WebSysDescription ("The method used to create the layout.")]\r
145                 public virtual RepeatLayout RepeatLayout\r
146                 {\r
147                         get\r
148                         {\r
149                                 object o = ViewState["RepeatLayout"];\r
150                                 if(o != null)\r
151                                         return (RepeatLayout)o;\r
152                                 return RepeatLayout.Table;\r
153                         }\r
154                         set\r
155                         {\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
159                         }\r
160                 }\r
161
162 #if !NET_2_0\r
163                 [Bindable (true)]\r
164 #endif\r
165                 [DefaultValue (typeof (TextAlign), "Right"), WebCategory ("Appearance")]
166                 [WebSysDescription ("The alignment of the RadioButton text.")]\r
167                 public virtual TextAlign TextAlign\r
168                 {\r
169                         get\r
170                         {\r
171                                 object o = ViewState["TextAlign"];\r
172                                 if(o != null)\r
173                                         return (TextAlign)o;\r
174                                 return TextAlign.Right;\r
175                         }\r
176                         set\r
177                         {\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
181                         }\r
182                 }\r
183 \r
184                 protected override Style CreateControlStyle()\r
185                 {\r
186                         return new TableStyle(ViewState);\r
187                 }\r
188 \r
189                 protected override void Render(HtmlTextWriter writer)\r
190                 {\r
191                         RepeatInfo info = new RepeatInfo();\r
192                         Style cStyle = (ControlStyleCreated ? ControlStyle : null);\r
193                         bool dirty = false;\r
194                         tabIndex = TabIndex;\r
195                         if(tabIndex != 0)\r
196                         {\r
197                                 dirty = !ViewState.IsItemDirty("TabIndex");\r
198                                 TabIndex = 0;\r
199                         }\r
200                         info.RepeatColumns = RepeatColumns;\r
201                         info.RepeatDirection = RepeatDirection;\r
202                         info.RepeatLayout = RepeatLayout;\r
203                         info.RenderRepeater(writer, this, cStyle, this);\r
204                         if(tabIndex != 0)\r
205                         {\r
206                                 TabIndex = tabIndex;\r
207                         }\r
208                         if(dirty)\r
209                         {\r
210                                 ViewState.SetItemDirty("TabIndex", false);\r
211                         }\r
212                 }\r
213
214                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
215                 {
216                         string value = postCollection [postDataKey];
217                         int c = Items.Count;
218                         for (int i = 0; i < c; i++) {
219                                 if (Items [i].Value != value)
220                                         continue;
221
222                                 if (i != SelectedIndex) {
223                                         SelectedIndex = i;
224                                         selectionIndexChanged = true;
225                                 }
226
227                                 return true;
228                         }
229
230                         return false;
231                 }
232
233                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
234                 {\r
235                         if(selectionIndexChanged)\r
236                                 OnSelectedIndexChanged(EventArgs.Empty);\r
237                 }\r
238 \r
239                 Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)\r
240                 {\r
241                         return null;\r
242                 }\r
243 \r
244                 void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,\r
245                                                  int repeatIndex,\r
246                                                  RepeatInfo repeatInfo,\r
247                                                  HtmlTextWriter writer)\r
248                 {\r
249                         /* Create a new RadioButton as if it was defined in the page and render it */\r
250                         RadioButton button = new RadioButton ();\r
251                         button.Page = Page;
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
263                 }\r
264 \r
265                 bool IRepeatInfoUser.HasFooter\r
266                 {\r
267                         get\r
268                         {\r
269                                 return false;\r
270                         }\r
271                 }\r
272 \r
273                 bool IRepeatInfoUser.HasHeader\r
274                 {\r
275                         get\r
276                         {\r
277                                 return false;\r
278                         }\r
279                 }\r
280 \r
281                 bool IRepeatInfoUser.HasSeparators\r
282                 {\r
283                         get\r
284                         {\r
285                                 return false;\r
286                         }\r
287                 }\r
288 \r
289                 int IRepeatInfoUser.RepeatedItemCount\r
290                 {\r
291                         get\r
292                         {\r
293                                 return Items.Count;\r
294                         }\r
295                 }\r
296         }\r
297 }\r