2004-09-12 Gonzalo Paniagua Javier <gonzalo@ximian.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                 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
54                 [WebSysDescription ("The border left within a RadioButton.")]\r
55                 public virtual int CellPadding\r
56                 {\r
57                         get\r
58                         {\r
59                                 if(ControlStyleCreated)\r
60                                 {\r
61                                         return (int)(((TableStyle)ControlStyle).CellPadding);\r
62                                 }\r
63                                 return -1;\r
64                         }\r
65                         set {
66                                 if (value < -1)
67                                         throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");\r
68                                 ((TableStyle)ControlStyle).CellPadding = value;\r
69                         }\r
70                 }\r
71
72                 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
73                 [WebSysDescription ("The border left between RadioButtons.")]\r
74                 public virtual int CellSpacing\r
75                 {\r
76                         get\r
77                         {\r
78                                 if(ControlStyleCreated)\r
79                                 {\r
80                                         return (int)(((TableStyle)ControlStyle).CellSpacing);\r
81                                 }\r
82                                 return -1;\r
83                         }\r
84                         set {
85                                 if (value < -1)
86                                         throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");\r
87                                 ((TableStyle)ControlStyle).CellSpacing = value;\r
88                         }\r
89                 }\r
90
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
94                 {\r
95                         get\r
96                         {\r
97                                 object o = ViewState["RepeatColumns"];\r
98                                 if(o != null)\r
99                                         return (int)o;\r
100                                 return 0;\r
101                         }\r
102                         set {\r
103                                 if (value < 0)\r
104                                         throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");\r
105                                 ViewState["RepeatColumns"] = value;\r
106                         }\r
107                 }\r
108
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
112                 {\r
113                         get\r
114                         {\r
115                                 object o = ViewState["RepeatDirection"];\r
116                                 if(o != null)\r
117                                         return (RepeatDirection)o;\r
118                                 return RepeatDirection.Vertical;\r
119                         }\r
120                         set\r
121                         {\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
125                         }\r
126                 }\r
127
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
131                 {\r
132                         get\r
133                         {\r
134                                 object o = ViewState["RepeatLayout"];\r
135                                 if(o != null)\r
136                                         return (RepeatLayout)o;\r
137                                 return RepeatLayout.Table;\r
138                         }\r
139                         set\r
140                         {\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
144                         }\r
145                 }\r
146
147                 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
148                 [WebSysDescription ("The alignment of the RadioButton text.")]\r
149                 public virtual TextAlign TextAlign\r
150                 {\r
151                         get\r
152                         {\r
153                                 object o = ViewState["TextAlign"];\r
154                                 if(o != null)\r
155                                         return (TextAlign)o;\r
156                                 return TextAlign.Right;\r
157                         }\r
158                         set\r
159                         {\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
163                         }\r
164                 }\r
165 \r
166                 protected override Style CreateControlStyle()\r
167                 {\r
168                         return new TableStyle(ViewState);\r
169                 }\r
170 \r
171                 protected override void Render(HtmlTextWriter writer)\r
172                 {\r
173                         RepeatInfo info = new RepeatInfo();\r
174                         Style cStyle = (ControlStyleCreated ? ControlStyle : null);\r
175                         bool dirty = false;\r
176                         tabIndex = TabIndex;\r
177                         if(tabIndex != 0)\r
178                         {\r
179                                 dirty = !ViewState.IsItemDirty("TabIndex");\r
180                                 TabIndex = 0;\r
181                         }\r
182                         info.RepeatColumns = RepeatColumns;\r
183                         info.RepeatDirection = RepeatDirection;\r
184                         info.RepeatLayout = RepeatLayout;\r
185                         info.RenderRepeater(writer, this, cStyle, this);\r
186                         if(tabIndex != 0)\r
187                         {\r
188                                 TabIndex = tabIndex;\r
189                         }\r
190                         if(dirty)\r
191                         {\r
192                                 ViewState.SetItemDirty("TabIndex", false);\r
193                         }\r
194                 }\r
195
196                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
197                 {
198                         string value = postCollection [postDataKey];
199                         int c = Items.Count;
200                         for (int i = 0; i < c; i++) {
201                                 if (Items [i].Value != value)
202                                         continue;
203
204                                 if (i != SelectedIndex) {
205                                         SelectedIndex = i;
206                                         selectionIndexChanged = true;
207                                 }
208
209                                 return true;
210                         }
211
212                         return false;
213                 }
214
215                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
216                 {\r
217                         if(selectionIndexChanged)\r
218                                 OnSelectedIndexChanged(EventArgs.Empty);\r
219                 }\r
220 \r
221                 Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)\r
222                 {\r
223                         return null;\r
224                 }\r
225 \r
226                 void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,\r
227                                                  int repeatIndex,\r
228                                                  RepeatInfo repeatInfo,\r
229                                                  HtmlTextWriter writer)\r
230                 {\r
231                         /* Create a new RadioButton as if it was defined in the page and render it */\r
232                         RadioButton button = new RadioButton ();\r
233                         button.Page = Page;
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
245                 }\r
246 \r
247                 bool IRepeatInfoUser.HasFooter\r
248                 {\r
249                         get\r
250                         {\r
251                                 return false;\r
252                         }\r
253                 }\r
254 \r
255                 bool IRepeatInfoUser.HasHeader\r
256                 {\r
257                         get\r
258                         {\r
259                                 return false;\r
260                         }\r
261                 }\r
262 \r
263                 bool IRepeatInfoUser.HasSeparators\r
264                 {\r
265                         get\r
266                         {\r
267                                 return false;\r
268                         }\r
269                 }\r
270 \r
271                 int IRepeatInfoUser.RepeatedItemCount\r
272                 {\r
273                         get\r
274                         {\r
275                                 return Items.Count;\r
276                         }\r
277                 }\r
278         }\r
279 }\r