New tests.
[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-2010 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 using System.Web.Util;
34
35 namespace System.Web.UI.HtmlControls {
36
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         // attributes
41         [DefaultEventAttribute ("ServerClick")]
42         [SupportsEventValidation]
43         public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler 
44         {
45                 static readonly object ServerClickEvent = new object();
46
47                 public HtmlInputButton () : this ("button")
48                 {
49                 }
50
51                 public HtmlInputButton (string type) :  base (type)
52                 {
53                 }
54
55                 [DefaultValue(true)]
56                 [WebSysDescription("")]
57                 [WebCategory("Behavior")]
58                 public virtual bool CausesValidation {
59                         get {
60                                 string flag = Attributes["CausesValidation"];
61
62                                 if (flag == null)
63                                         return true;
64
65                                 return Boolean.Parse (flag);
66                         }
67                         set {
68                                 Attributes ["CausesValidation"] = value.ToString();
69                         }
70                 }
71
72                 [DefaultValue ("")]
73                 public virtual string ValidationGroup
74                 {
75                         get {
76                                 string group = Attributes["ValidationGroup"];
77
78                                 if (group == null)
79                                         return "";
80
81                                 return group;
82                         }
83                         set {
84                                 if (value == null)
85                                         Attributes.Remove ("ValidationGroup");
86                                 else
87                                         Attributes["ValidationGroup"] = value;
88                         }
89                 }
90
91                 void RaisePostBackEventInternal (string eventArgument)
92                 {
93                         ValidateEvent (UniqueID, eventArgument);
94                         if (CausesValidation)
95                                 Page.Validate (ValidationGroup);
96                         
97                         if (String.Compare (Type, "reset", true, Helpers.InvariantCulture) != 0)
98                                 OnServerClick (EventArgs.Empty);
99                         else
100                                 ResetForm (FindForm ());
101                 }
102
103                 HtmlForm FindForm ()
104                 {
105                         Page p = Page;
106                         if (p != null)
107                                 return p.Form;
108
109                         return null;
110                 }
111                 
112                 void ResetForm (HtmlForm form)
113                 {
114                         if (form == null || !form.HasControls ())
115                                 return;
116                         
117                         ResetChildrenValues (form.Controls);
118                 }
119
120                 void ResetChildrenValues (ControlCollection children)
121                 {
122                         foreach (Control child in children) {
123                                 if (child == null)
124                                         continue;
125                                 
126                                 if (child.HasControls ())
127                                         ResetChildrenValues (child.Controls);
128                                 ResetChildValue (child);
129                         }
130                 }
131
132                 void ResetChildValue (Control child)
133                 {
134                         Type type = child.GetType ();
135                         object[] attributes = type.GetCustomAttributes (false);
136                         if (attributes == null || attributes.Length == 0)
137                                 return;
138
139                         string defaultProperty = null;
140                         DefaultPropertyAttribute defprop;
141                         
142                         foreach (object attr in attributes) {
143                                 defprop = attr as DefaultPropertyAttribute;
144                                 if (defprop == null)
145                                         continue;
146                                 defaultProperty = defprop.Name;
147                                 break;
148                         }
149
150                         if (defaultProperty == null || defaultProperty.Length == 0)
151                                 return;
152
153                         PropertyInfo pi = null;
154                         try {
155                                 pi = type.GetProperty (defaultProperty,
156                                                        BindingFlags.Instance |
157                                                        BindingFlags.Public |
158                                                        BindingFlags.Static |
159                                                        BindingFlags.IgnoreCase);
160                         } catch (Exception) {
161                                 // ignore
162                         }
163                         if (pi == null || !pi.CanWrite)
164                                 return;
165                         
166                         attributes = pi.GetCustomAttributes (false);
167                         if (attributes == null || attributes.Length == 0)
168                                 return;
169
170                         DefaultValueAttribute defval = null;
171                         object value = null;
172                         
173                         foreach (object attr in attributes) {
174                                 defval = attr as DefaultValueAttribute;
175                                 if (defval == null)
176                                         continue;
177                                 value = defval.Value;
178                                 break;
179                         }
180                         
181                         if (value == null || pi.PropertyType != value.GetType ())
182                                 return;
183
184                         try {
185                                 pi.SetValue (child, value, null);
186                         } catch (Exception) {
187                                 // ignore
188                         }
189                 }
190
191                 protected virtual void RaisePostBackEvent (string eventArgument)
192                 {
193                         RaisePostBackEventInternal (eventArgument);
194                 }
195                 
196                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
197                 {
198                         RaisePostBackEvent (eventArgument);
199                 }
200
201                 protected internal override void OnPreRender (EventArgs e)
202                 {
203                         base.OnPreRender (e);
204                         if (Events [ServerClickEvent] != null)
205                                 Page.RequiresPostBackScript ();
206                 }
207
208                 protected virtual void OnServerClick (EventArgs e)
209                 {
210                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
211                         if (server_click != null)
212                                 server_click (this, e);
213                 }
214
215                 protected override void RenderAttributes (HtmlTextWriter writer)
216                 {
217                         CultureInfo inv = Helpers.InvariantCulture;
218                         string input_type = Type;
219                         if (0 != String.Compare (input_type, "reset", true, inv) &&
220                                 ((0 == String.Compare (input_type, "submit", true, inv)) ||
221                                 (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
222
223                                 string onclick = String.Empty;
224                                 if (Attributes ["onclick"] != null) {
225                                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
226                                         Attributes.Remove ("onclick");
227                                 }
228
229                                 Page page = Page;
230                                 if (page != null) {
231                                         PostBackOptions options = GetPostBackOptions ();
232                                         onclick += page.ClientScript.GetPostBackEventReference (options, true);
233                                 }
234
235                                 if (onclick.Length > 0) {
236                                         writer.WriteAttribute ("onclick", onclick, true);
237                                         writer.WriteAttribute ("language", "javascript");
238                                 }
239                         }
240
241                         Attributes.Remove ("CausesValidation");
242                         // LAMESPEC: MS doesn't actually remove this
243                         //attribute.  it shows up in the rendered
244                         //output.
245
246                         // Attributes.Remove("ValidationGroup");
247                         base.RenderAttributes (writer);
248                 }
249
250                 PostBackOptions GetPostBackOptions ()
251                 {
252                         Page page = Page;
253                         PostBackOptions options = new PostBackOptions (this);
254                         options.ValidationGroup = null;
255                         options.ActionUrl = null;
256                         options.Argument = String.Empty;
257                         options.RequiresJavaScriptProtocol = false;
258                         options.ClientSubmit = (0 != String.Compare (Type, "submit", true, Helpers.InvariantCulture));
259                         options.PerformValidation = CausesValidation && page != null && page.Validators.Count > 0;
260                         if (options.PerformValidation)
261                                 options.ValidationGroup = ValidationGroup;
262
263                         return options;
264                 }
265
266                 [WebSysDescription("")]
267                 [WebCategory("Action")]
268                 public event EventHandler ServerClick {
269                         add { Events.AddHandler (ServerClickEvent, value); }
270                         remove { Events.RemoveHandler (ServerClickEvent, value); }
271                 }
272         }       
273 }
274