2 // System.Web.UI.HtmlControls.HtmlInputButton.cs
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2005 Novell, Inc.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Reflection;
32 using System.Security.Permissions;
33 using System.Web.Util;
35 namespace System.Web.UI.HtmlControls {
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [DefaultEventAttribute ("ServerClick")]
43 [SupportsEventValidation]
45 public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler
47 static readonly object ServerClickEvent = new object();
49 public HtmlInputButton () : this ("button")
53 public HtmlInputButton (string type) : base (type)
58 [WebSysDescription("")]
59 [WebCategory("Behavior")]
65 bool CausesValidation {
67 string flag = Attributes["CausesValidation"];
72 return Boolean.Parse (flag);
75 Attributes ["CausesValidation"] = value.ToString();
81 public virtual string ValidationGroup
84 string group = Attributes["ValidationGroup"];
93 Attributes.Remove ("ValidationGroup");
95 Attributes["ValidationGroup"] = value;
100 void RaisePostBackEventInternal (string eventArgument)
103 ValidateEvent (UniqueID, eventArgument);
105 if (CausesValidation) {
107 Page.Validate (ValidationGroup);
113 if (String.Compare (Type, "reset", true, Helpers.InvariantCulture) != 0)
114 OnServerClick (EventArgs.Empty);
116 ResetForm (FindForm ());
139 void ResetForm (HtmlForm form)
141 if (form == null || !form.HasControls ())
144 ResetChildrenValues (form.Controls);
147 void ResetChildrenValues (ControlCollection children)
149 foreach (Control child in children) {
153 if (child.HasControls ())
154 ResetChildrenValues (child.Controls);
155 ResetChildValue (child);
159 void ResetChildValue (Control child)
161 Type type = child.GetType ();
162 object[] attributes = type.GetCustomAttributes (false);
163 if (attributes == null || attributes.Length == 0)
166 string defaultProperty = null;
167 DefaultPropertyAttribute defprop;
169 foreach (object attr in attributes) {
170 defprop = attr as DefaultPropertyAttribute;
173 defaultProperty = defprop.Name;
177 if (defaultProperty == null || defaultProperty.Length == 0)
180 PropertyInfo pi = null;
182 pi = type.GetProperty (defaultProperty,
183 BindingFlags.Instance |
184 BindingFlags.Public |
185 BindingFlags.Static |
186 BindingFlags.IgnoreCase);
187 } catch (Exception) {
190 if (pi == null || !pi.CanWrite)
193 attributes = pi.GetCustomAttributes (false);
194 if (attributes == null || attributes.Length == 0)
197 DefaultValueAttribute defval = null;
200 foreach (object attr in attributes) {
201 defval = attr as DefaultValueAttribute;
204 value = defval.Value;
208 if (value == null || pi.PropertyType != value.GetType ())
212 pi.SetValue (child, value, null);
213 } catch (Exception) {
218 protected virtual void RaisePostBackEvent (string eventArgument)
220 RaisePostBackEventInternal (eventArgument);
224 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
227 RaisePostBackEvent (eventArgument);
229 RaisePostBackEventInternal (eventArgument);
238 override void OnPreRender (EventArgs e)
240 base.OnPreRender (e);
241 if (Events [ServerClickEvent] != null)
242 Page.RequiresPostBackScript ();
245 protected virtual void OnServerClick (EventArgs e)
247 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
248 if (server_click != null)
249 server_click (this, e);
253 bool RenderOnClick ()
255 if (Page == null || !CausesValidation)
258 CultureInfo inv = Helpers.InvariantCulture;
259 string input_type = Type;
260 if (0 == String.Compare (input_type, "submit", true, inv) &&
261 Page.Validators.Count > 0)
264 if (0 == String.Compare (input_type, "button", true, inv) &&
265 Events [ServerClickEvent] != null)
272 protected override void RenderAttributes (HtmlTextWriter writer)
275 CultureInfo inv = Helpers.InvariantCulture;
276 string input_type = Type;
277 if (0 != String.Compare (input_type, "reset", true, inv) &&
278 ((0 == String.Compare (input_type, "submit", true, inv)) ||
279 (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
281 string onclick = String.Empty;
282 if (Attributes ["onclick"] != null) {
283 onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
284 Attributes.Remove ("onclick");
287 PostBackOptions options = GetPostBackOptions ();
288 onclick += Page.ClientScript.GetPostBackEventReference (options, true);
291 if (onclick.Length > 0) {
292 writer.WriteAttribute ("onclick", onclick, true);
293 writer.WriteAttribute ("language", "javascript");
297 if (RenderOnClick ()) {
299 ClientScriptManager csm = new ClientScriptManager (Page);
300 if (Page.AreValidatorsUplevel ()) {
301 oc = csm.GetClientValidationEvent ();
302 } else if (Events [ServerClickEvent] != null) {
303 oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
307 writer.WriteAttribute ("language", "javascript");
308 writer.WriteAttribute ("onclick", oc, true);
313 Attributes.Remove ("CausesValidation");
315 // LAMESPEC: MS doesn't actually remove this
316 //attribute. it shows up in the rendered
319 // Attributes.Remove("ValidationGroup");
321 base.RenderAttributes (writer);
325 PostBackOptions GetPostBackOptions () {
326 PostBackOptions options = new PostBackOptions (this);
327 options.ValidationGroup = null;
328 options.ActionUrl = null;
329 options.Argument = String.Empty;
330 options.RequiresJavaScriptProtocol = false;
331 options.ClientSubmit = (0 != String.Compare (Type, "submit", true, Helpers.InvariantCulture));
332 options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
333 if (options.PerformValidation)
334 options.ValidationGroup = ValidationGroup;
340 [WebSysDescription("")]
341 [WebCategory("Action")]
342 public event EventHandler ServerClick {
343 add { Events.AddHandler (ServerClickEvent, value); }
344 remove { Events.RemoveHandler (ServerClickEvent, value); }