importing messaging-2008 branch to trunk [continued]
[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                 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 (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 && !Disabled) {
109                                 Page.RegisterRequiresPostBack (this);
110 #if NET_2_0
111                                 Page.RegisterEnabledControl (this);
112 #endif
113                         }
114                 }
115
116                 protected virtual void OnServerChange (EventArgs e)
117                 {
118                         EventHandler handler = (EventHandler)Events[EventServerChange];
119
120                         if (handler != null) {
121                                 handler (this, e);
122                         }
123                 }
124
125                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
126                 {
127                         string postedValue = postCollection[postDataKey];
128                         bool postedBool = ((postedValue != null) &&
129                                            (postedValue.Length > 0));
130
131                         if (Checked != postedBool) {
132                                 Checked = postedBool;
133                                 return (true);
134                         }
135                         
136                         return (false);
137                 }
138
139                 void RaisePostDataChangedEventInternal ()
140                 {
141                         OnServerChange (EventArgs.Empty);
142                 }
143
144 #if NET_2_0
145                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
146                 {
147                         return LoadPostDataInternal (postDataKey, postCollection);
148                 }
149
150                 protected virtual void RaisePostDataChangedEvent ()
151                 {
152                         ValidateEvent (UniqueID, String.Empty);
153                         RaisePostDataChangedEventInternal ();
154                 }
155 #endif
156                 
157                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
158                 {
159 #if NET_2_0
160                         return LoadPostData (postDataKey, postCollection);
161 #else
162                         return LoadPostDataInternal (postDataKey, postCollection);
163 #endif
164                 }
165
166                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
167                 {
168 #if NET_2_0
169                         RaisePostDataChangedEvent();
170 #else
171                         RaisePostDataChangedEventInternal ();
172 #endif
173                 }
174         }
175 }