Merge pull request #853 from echampet/onclick
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxList.cs
1 //
2 // System.Web.UI.WebControls.CheckBoxList.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Globalization;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33 using System.Web.Util;
34
35 namespace System.Web.UI.WebControls
36 {
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class CheckBoxList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler 
41         {
42                 CheckBox check_box;
43
44                 public CheckBoxList ()
45                 {
46                         check_box = new CheckBox ();
47                         Controls.Add (check_box);
48                 }
49
50                 [DefaultValue(-1)]
51                 [WebSysDescription ("")]
52                 [WebCategory ("Layout")]
53                 public virtual int CellPadding {
54                         get { return TableStyle.CellPadding; }
55                         set { TableStyle.CellPadding = value; }
56                 }
57
58                 [DefaultValue(-1)]
59                 [WebSysDescription ("")]
60                 [WebCategory ("Layout")]
61                 public virtual int CellSpacing {
62                         get { return TableStyle.CellSpacing; }
63                         set { TableStyle.CellSpacing = value; }
64                 }
65
66                 [DefaultValue(0)]
67                 [WebSysDescription ("")]
68                 [WebCategory ("Layout")]
69                 public virtual int RepeatColumns {
70                         get { return ViewState.GetInt ("RepeatColumns", 0); }
71                         set {
72                                 if (value < 0)
73                                         throw new ArgumentOutOfRangeException ("value");
74                                 ViewState ["RepeatColumns"] = value;
75                         }
76                 }
77
78                 [DefaultValue(RepeatDirection.Vertical)]
79                 [WebSysDescription ("")]
80                 [WebCategory ("Layout")]
81                 public virtual RepeatDirection RepeatDirection {
82                         get { return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical); }
83                         set {
84                                 if (value < RepeatDirection.Horizontal ||
85                                                 value > RepeatDirection.Vertical)
86                                         throw new ArgumentOutOfRangeException ("value");
87                                 ViewState ["RepeatDirection"] = value;
88                         }
89                 }
90
91                 [DefaultValue(RepeatLayout.Table)]
92                 [WebSysDescription ("")]
93                 [WebCategory ("Layout")]
94                 public virtual RepeatLayout RepeatLayout {
95                         get { return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table); }
96                         set {
97                                 bool outOfRange;
98                                 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.OrderedList;
99                                 if (outOfRange)
100                                         throw new ArgumentOutOfRangeException ("value");
101                                 ViewState ["RepeatLayout"] = value;
102                         }
103                 }
104
105                 [DefaultValue(TextAlign.Right)]
106                 [WebSysDescription ("")]
107                 [WebCategory ("Appearance")]
108                 public virtual TextAlign TextAlign {
109                         get { return (TextAlign) ViewState.GetInt ("TextAlign", (int) TextAlign.Right); }
110                         set {
111                                 if (value < TextAlign.Left || value > TextAlign.Right)
112                                         throw new ArgumentOutOfRangeException ("value");
113                                 ViewState ["TextAlign"] = value;
114                         }
115                 }
116
117                 TableStyle TableStyle {
118                         get { return (TableStyle) ControlStyle; }
119                 }
120
121                 protected override Style CreateControlStyle ()
122                 {
123                         return new TableStyle (ViewState);
124                 }
125
126                 protected override Control FindControl (string id, int pathOffset)
127                 {
128                         // Always, or in just all my tests?
129                         return this;
130                 }
131
132                 protected internal override void OnPreRender (EventArgs e)
133                 {
134                         base.OnPreRender (e);
135
136                         // Register all of the checked controls so we can
137                         // find out when they are unchecked.
138                         Page page = Page;
139                         for (int i = 0; i < Items.Count; i++) {
140                                 if (Items [i].Selected) {
141                                         check_box.ID = i.ToString (Helpers.InvariantCulture);
142                                         if (page != null)
143                                                 page.RegisterRequiresPostBack (check_box);
144                                 }
145                         }
146                 }
147
148                 protected internal override void Render (HtmlTextWriter writer)
149                 {
150                         if (Items.Count == 0)
151                                 return;
152
153                         RepeatInfo ri = new RepeatInfo ();
154                         ri.RepeatColumns = RepeatColumns;
155                         ri.RepeatDirection = RepeatDirection;
156                         ri.RepeatLayout = RepeatLayout;
157
158                         short ti = 0;
159                         if (TabIndex != 0) {
160                                 check_box.TabIndex = TabIndex;
161                                 ti = TabIndex;
162                                 TabIndex = 0;
163                         }
164
165                         string ak = AccessKey;
166                         check_box.AccessKey = ak;
167                         this.AccessKey = null;
168
169                         ri.RenderRepeater (writer, this, TableStyle, this);
170
171                         if (ti != 0)
172                                 TabIndex = ti;
173                         this.AccessKey = ak;
174                 }
175
176                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
177                 {
178                         if (!IsEnabled)
179                                 return false;
180
181                         EnsureDataBound ();
182                         int checkbox = -1;
183
184                         try {
185                                 string id = postDataKey.Substring (ClientID.Length + 1);
186                                 if (Char.IsDigit (id [0]))
187                                         checkbox = Int32.Parse (id, Helpers.InvariantCulture);
188                         } catch {
189                                 return false;
190                         }
191
192                         if (checkbox == -1)
193                                 return false;
194
195                         string val = postCollection [postDataKey];
196                         bool ischecked = val == "on";
197                         ListItem item = Items [checkbox];
198                         if (item.Enabled) {
199                                 if (ischecked && !item.Selected) {
200                                         item.Selected = true;
201                                         return true;
202                                 } else if (!ischecked && item.Selected) {
203                                         item.Selected = false;
204                                         return true;
205                                 }
206                         }
207                         
208                         return false;
209                 }
210
211                 protected virtual void RaisePostDataChangedEvent ()
212                 {
213                         if (CausesValidation) {
214                                 Page page = Page;
215                                 if (page != null)
216                                         page.Validate (ValidationGroup);
217                         }
218                         
219                         OnSelectedIndexChanged (EventArgs.Empty);
220                 }
221
222                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
223                 {
224                         return LoadPostData (postDataKey, postCollection);
225                 }
226
227                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
228                 {
229                         RaisePostDataChangedEvent ();
230                 }
231
232                 protected virtual bool HasFooter  {
233                         get { return false; }
234                 }
235
236                 bool IRepeatInfoUser.HasFooter {
237                         get { return HasFooter; }
238                 }
239
240                 protected virtual bool HasHeader {
241                         get { return false; }
242                 }
243
244                 bool IRepeatInfoUser.HasHeader {
245                         get { return HasHeader; }
246                 }
247
248                 protected virtual bool HasSeparators {
249                         get { return false; }
250                 }
251
252                 bool IRepeatInfoUser.HasSeparators {
253                         get { return HasSeparators; }
254                 }
255
256                 protected virtual int RepeatedItemCount {
257                         get { return Items.Count; }
258                 }
259
260                 int IRepeatInfoUser.RepeatedItemCount {
261                         get { return RepeatedItemCount; }
262                 }
263
264                 protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
265                 {
266                         return null;
267                 }
268
269                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
270                 {
271                         return GetItemStyle (itemType, repeatIndex);
272                 }
273
274                 protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
275                 {
276                         ListItem item = Items [repeatIndex];
277
278                         string cssClass = check_box.CssClass;
279                         if (!String.IsNullOrEmpty (cssClass))
280                                 check_box.CssClass = String.Empty;
281                         check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
282                         check_box.Text = item.Text;
283                         check_box.AutoPostBack = AutoPostBack;
284                         check_box.Checked = item.Selected;
285                         check_box.TextAlign = TextAlign;
286                         if (!IsEnabled)
287                                 check_box.Enabled = false;
288                         else
289                                 check_box.Enabled = item.Enabled;
290
291                         check_box.ValidationGroup = ValidationGroup;
292                         check_box.CausesValidation = CausesValidation;
293                         if (check_box.HasAttributes)
294                                 check_box.Attributes.Clear ();
295                         if (item.HasAttributes)
296                                 check_box.Attributes.CopyFrom (item.Attributes);
297                         if (!RenderingCompatibilityLessThan40) {
298                                 var attrs = check_box.InputAttributes;
299                         
300                                 attrs.Clear ();
301                                 attrs.Add ("value", item.Value);
302                         }
303                         check_box.RenderControl (writer);
304                 }
305
306                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
307                 {
308                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
309                 }
310
311                 internal override bool MultiSelectOk ()
312                 {
313                         return true;
314                 }
315         }
316 }