Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputButton.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlInputButton.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005 Novell, Inc.
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.Globalization;
31 using System.Reflection;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.HtmlControls {
35
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [DefaultEventAttribute ("ServerClick")]
41 #if NET_2_0
42         [SupportsEventValidation]
43 #endif
44         public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
45
46                 private static readonly object ServerClickEvent = new object();
47
48                 public HtmlInputButton () : this ("button")
49                 {
50                 }
51
52                 public HtmlInputButton (string type) :  base (type)
53                 {
54                 }
55
56                 [DefaultValue(true)]
57                 [WebSysDescription("")]
58                 [WebCategory("Behavior")]
59 #if NET_2_0
60                 public virtual
61 #else           
62                 public
63 #endif          
64                 bool CausesValidation {
65                         get {
66                                 string flag = Attributes["CausesValidation"];
67
68                                 if (flag == null)
69                                         return true;
70
71                                 return Boolean.Parse (flag);
72                         }
73                         set {
74                                 Attributes ["CausesValidation"] = value.ToString();
75                         }
76                 }
77
78 #if NET_2_0
79                 [DefaultValue ("")]
80                 public virtual string ValidationGroup
81                 {
82                         get {
83                                 string group = Attributes["ValidationGroup"];
84
85                                 if (group == null)
86                                         return "";
87
88                                 return group;
89                         }
90                         set {
91                                 if (value == null)
92                                         Attributes.Remove ("ValidationGroup");
93                                 else
94                                         Attributes["ValidationGroup"] = value;
95                         }
96                 }
97 #endif
98
99                 void RaisePostBackEventInternal (string eventArgument)
100                 {
101                         if (CausesValidation) {
102 #if NET_2_0
103                                 Page.Validate (ValidationGroup);
104 #else
105                                 Page.Validate ();
106 #endif
107                         }
108                         
109                         if (String.Compare (Type, "reset", true, CultureInfo.InvariantCulture) != 0)
110                                 OnServerClick (EventArgs.Empty);
111                         else
112                                 ResetForm (FindForm ());
113                 }
114
115                 HtmlForm FindForm ()
116                 {
117 #if NET_2_0
118                         return Page.Form;
119 #else
120                         HtmlForm ret = null;
121                         Control p = Parent;
122                         while (p != null) {
123                                 ret = p as HtmlForm;
124                                 if (ret == null) {
125                                         p = p.Parent;
126                                         continue;
127                                 }
128                                 return ret;
129                         }
130
131                         return null;
132 #endif
133                 }
134                 
135                 void ResetForm (HtmlForm form)
136                 {
137                         if (form == null || !form.HasControls ())
138                                 return;
139                         
140                         ResetChildrenValues (form.Controls);
141                 }
142
143                 void ResetChildrenValues (ControlCollection children)
144                 {
145                         foreach (Control child in children) {
146                                 if (child == null)
147                                         continue;
148                                 
149                                 if (child.HasControls ())
150                                         ResetChildrenValues (child.Controls);
151                                 ResetChildValue (child);
152                         }
153                 }
154
155                 void ResetChildValue (Control child)
156                 {
157                         Type type = child.GetType ();
158                         object[] attributes = type.GetCustomAttributes (false);
159                         if (attributes == null || attributes.Length == 0)
160                                 return;
161
162                         string defaultProperty = null;
163                         DefaultPropertyAttribute defprop;
164                         
165                         foreach (object attr in attributes) {
166                                 defprop = attr as DefaultPropertyAttribute;
167                                 if (defprop == null)
168                                         continue;
169                                 defaultProperty = defprop.Name;
170                                 break;
171                         }
172
173                         if (defaultProperty == null || defaultProperty.Length == 0)
174                                 return;
175
176                         PropertyInfo pi = null;
177                         try {
178                                 pi = type.GetProperty (defaultProperty,
179                                                        BindingFlags.Instance |
180                                                        BindingFlags.Public |
181                                                        BindingFlags.Static |
182                                                        BindingFlags.IgnoreCase);
183                         } catch (Exception) {
184                                 // ignore
185                         }
186                         if (pi == null || !pi.CanWrite)
187                                 return;
188                         
189                         attributes = pi.GetCustomAttributes (false);
190                         if (attributes == null || attributes.Length == 0)
191                                 return;
192
193                         DefaultValueAttribute defval = null;
194                         object value = null;
195                         
196                         foreach (object attr in attributes) {
197                                 defval = attr as DefaultValueAttribute;
198                                 if (defval == null)
199                                         continue;
200                                 value = defval.Value;
201                                 break;
202                         }
203                         
204                         if (value == null || pi.PropertyType != value.GetType ())
205                                 return;
206
207                         try {
208                                 pi.SetValue (child, value, null);
209                         } catch (Exception) {
210                                 // ignore
211                         }
212                 }
213 #if NET_2_0
214                 protected virtual void RaisePostBackEvent (string eventArgument)
215                 {
216                         RaisePostBackEventInternal (eventArgument);
217                 }
218 #endif
219                 
220                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
221                 {
222 #if NET_2_0
223                         RaisePostBackEvent (eventArgument);
224 #else
225                         RaisePostBackEventInternal (eventArgument);
226 #endif
227                 }
228
229 #if NET_2_0
230                 protected internal
231 #else           
232                 protected
233 #endif          
234                 override void OnPreRender (EventArgs e)
235                 {
236                         base.OnPreRender (e);
237                         if (Events [ServerClickEvent] != null)
238                                 Page.RequiresPostBackScript ();
239                 }
240
241                 protected virtual void OnServerClick (EventArgs e)
242                 {
243                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
244                         if (server_click != null)
245                                 server_click (this, e);
246                 }
247
248 #if !NET_2_0
249                 bool RenderOnClick ()
250                 {
251                         if (Page == null || !CausesValidation)
252                                 return false;
253
254                         CultureInfo inv = CultureInfo.InvariantCulture;
255                         string input_type = Type;
256                         if (0 == String.Compare (input_type, "submit", true, inv) &&
257                                 Page.Validators.Count > 0)
258                                 return true;
259
260                         if (0 == String.Compare (input_type, "button", true, inv) &&
261                                 Events [ServerClickEvent] != null)
262                                 return true;
263
264                         return false;
265                 }
266 #endif
267
268                 protected override void RenderAttributes (HtmlTextWriter writer)
269                 {
270 #if NET_2_0
271                         CultureInfo inv = CultureInfo.InvariantCulture;
272                         string input_type = Type;
273                         if (0 != String.Compare (input_type, "reset", true, inv) &&
274                                 ((0 == String.Compare (input_type, "submit", true, inv)) ||
275                                 (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
276
277                                 string onclick = String.Empty;
278                                 if (Attributes ["onclick"] != null) {
279                                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
280                                         Attributes.Remove ("onclick");
281                                 }
282                                 if (Page != null) {
283                                         PostBackOptions options = GetPostBackOptions ();
284                                         Page.ClientScript.RegisterForEventValidation (options);
285                                         onclick += Page.ClientScript.GetPostBackEventReference (options);
286                                 }
287
288                                 if (onclick.Length > 0) {
289                                         writer.WriteAttribute ("onclick", onclick, true);
290                                         writer.WriteAttribute ("language", "javascript");
291                                 }
292                         }
293 #else
294                         if (RenderOnClick ()) {
295                                 string oc = null;
296                                 ClientScriptManager csm = new ClientScriptManager (Page);
297                                 if (Page.AreValidatorsUplevel ()) {
298                                         oc = csm.GetClientValidationEvent ();
299                                 } else if (Events [ServerClickEvent] != null) {
300                                         oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
301                                 }
302                                 
303                                 if (oc != null) {
304                                         writer.WriteAttribute ("language", "javascript");
305                                         writer.WriteAttribute ("onclick", oc, true);
306                                 }
307                         }
308 #endif
309
310                         Attributes.Remove ("CausesValidation");
311 #if NET_2_0
312                         // LAMESPEC: MS doesn't actually remove this
313                         //attribute.  it shows up in the rendered
314                         //output.
315
316                         // Attributes.Remove("ValidationGroup");
317 #endif
318                         base.RenderAttributes (writer);
319                 }
320
321 #if NET_2_0
322                 PostBackOptions GetPostBackOptions () {
323                         PostBackOptions options = new PostBackOptions (this);
324                         options.ValidationGroup = null;
325                         options.ActionUrl = null;
326                         options.Argument = "";
327                         options.RequiresJavaScriptProtocol = false;
328                         options.ClientSubmit = (0 != String.Compare (Type, "submit", true, CultureInfo.InvariantCulture));
329                         options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
330                         if (options.PerformValidation)
331                                 options.ValidationGroup = ValidationGroup;
332
333                         return options;
334                 }
335 #endif
336
337                 [WebSysDescription("")]
338                 [WebCategory("Action")]
339                 public event EventHandler ServerClick {
340                         add { Events.AddHandler (ServerClickEvent, value); }
341                         remove { Events.RemoveHandler (ServerClickEvent, value); }
342                 }
343         }
344         
345 }
346