New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputCheckBox.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlInputCheckBox.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (C) 2005 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.ComponentModel;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.HtmlControls 
34 {
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [DefaultEvent ("ServerChange")]
40 #if NET_2_0
41         [SupportsEventValidation]
42 #endif
43         public class HtmlInputCheckBox : HtmlInputControl, IPostBackDataHandler
44         {
45                 public HtmlInputCheckBox () : base ("checkbox")
46                 {
47                 }
48
49                 [DefaultValue ("")]
50                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
51                 [WebSysDescription("")]
52                 [WebCategory("Misc")]
53 #if NET_2_0
54                 [TypeConverter (typeof(MinimizableAttributeTypeConverter))]
55 #endif
56                 public bool Checked
57                 {
58                         get {
59                                 string check = Attributes["checked"];
60
61                                 if (check == null) {
62                                         return (false);
63                                 }
64
65                                 return (true);
66                         }
67                         set {
68                                 if (value == false) {
69                                         Attributes.Remove ("checked");
70                                 } else {
71                                         Attributes["checked"] = "checked";
72                                 }
73                         }
74                 }
75                 
76                 private static readonly object EventServerChange = new object ();
77
78                 [WebSysDescription("")]
79                 [WebCategory("Action")]
80                 public event EventHandler ServerChange
81                 {
82                         add {
83                                 Events.AddHandler (EventServerChange, value);
84                         }
85                         remove {
86                                 Events.RemoveHandler (EventServerChange, value);
87                         }
88                 }
89
90 #if NET_2_0
91                 protected internal
92 #else
93                 protected
94 #endif
95                 override void OnPreRender (EventArgs e)
96                 {
97                         base.OnPreRender (e);
98
99                         if (Page != null) {
100                                 Page.RegisterRequiresPostBack (this);
101                         }
102                 }
103
104                 protected virtual void OnServerChange (EventArgs e)
105                 {
106                         EventHandler handler = (EventHandler)Events[EventServerChange];
107
108                         if (handler != null) {
109                                 handler (this, e);
110                         }
111                 }
112
113                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
114                 {
115                         string postedValue = postCollection[postDataKey];
116                         bool postedBool = ((postedValue != null) &&
117                                            (postedValue.Length > 0));
118
119                         if (Checked != postedBool) {
120                                 Checked = postedBool;
121                                 return (true);
122                         }
123                         
124                         return (false);
125                 }
126
127                 void RaisePostDataChangedEventInternal ()
128                 {
129                         OnServerChange (EventArgs.Empty);
130                 }
131
132 #if NET_2_0
133                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
134                 {
135                         return LoadPostDataInternal (postDataKey, postCollection);
136                 }
137
138                 protected virtual void RaisePostDataChangedEvent ()
139                 {
140                         RaisePostDataChangedEventInternal ();
141                 }
142 #endif
143                 
144                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
145                 {
146 #if NET_2_0
147                         return LoadPostData (postDataKey, postCollection);
148 #else
149                         return LoadPostDataInternal (postDataKey, postCollection);
150 #endif
151                 }
152
153                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
154                 {
155 #if NET_2_0
156                         RaisePostDataChangedEvent();
157 #else
158                         RaisePostDataChangedEventInternal ();
159 #endif
160                 }
161         }
162 }