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 override void RenderAttributes (HtmlTextWriter writer)
92                 {
93                         if (Page != null)
94                                 Page.ClientScript.RegisterForEventValidation (this.UniqueID);
95                         base.RenderAttributes (writer);
96                 }
97 #endif
98
99 #if NET_2_0
100                 protected internal
101 #else
102                 protected
103 #endif
104                 override void OnPreRender (EventArgs e)
105                 {
106                         base.OnPreRender (e);
107
108                         if (Page != null) {
109                                 Page.RegisterRequiresPostBack (this);
110                         }
111                 }
112
113                 protected virtual void OnServerChange (EventArgs e)
114                 {
115                         EventHandler handler = (EventHandler)Events[EventServerChange];
116
117                         if (handler != null) {
118                                 handler (this, e);
119                         }
120                 }
121
122                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
123                 {
124                         string postedValue = postCollection[postDataKey];
125                         bool postedBool = ((postedValue != null) &&
126                                            (postedValue.Length > 0));
127
128                         if (Checked != postedBool) {
129                                 Checked = postedBool;
130                                 return (true);
131                         }
132                         
133                         return (false);
134                 }
135
136                 void RaisePostDataChangedEventInternal ()
137                 {
138                         OnServerChange (EventArgs.Empty);
139                 }
140
141 #if NET_2_0
142                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
143                 {
144                         return LoadPostDataInternal (postDataKey, postCollection);
145                 }
146
147                 protected virtual void RaisePostDataChangedEvent ()
148                 {
149                         RaisePostDataChangedEventInternal ();
150                 }
151 #endif
152                 
153                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
154                 {
155 #if NET_2_0
156                         return LoadPostData (postDataKey, postCollection);
157 #else
158                         return LoadPostDataInternal (postDataKey, postCollection);
159 #endif
160                 }
161
162                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
163                 {
164 #if NET_2_0
165                         RaisePostDataChangedEvent();
166 #else
167                         RaisePostDataChangedEventInternal ();
168 #endif
169                 }
170         }
171 }