2 // System.Web.UI.HtmlControls.HtmlInputButton.cs
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2005-2010 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")]
42 [SupportsEventValidation]
43 public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler
45 static readonly object ServerClickEvent = new object();
47 public HtmlInputButton () : this ("button")
51 public HtmlInputButton (string type) : base (type)
56 [WebSysDescription("")]
57 [WebCategory("Behavior")]
58 public virtual bool CausesValidation {
60 string flag = Attributes["CausesValidation"];
65 return Boolean.Parse (flag);
68 Attributes ["CausesValidation"] = value.ToString();
73 public virtual string ValidationGroup
76 string group = Attributes["ValidationGroup"];
85 Attributes.Remove ("ValidationGroup");
87 Attributes["ValidationGroup"] = value;
91 void RaisePostBackEventInternal (string eventArgument)
93 ValidateEvent (UniqueID, eventArgument);
95 Page.Validate (ValidationGroup);
97 if (String.Compare (Type, "reset", true, Helpers.InvariantCulture) != 0)
98 OnServerClick (EventArgs.Empty);
100 ResetForm (FindForm ());
112 void ResetForm (HtmlForm form)
114 if (form == null || !form.HasControls ())
117 ResetChildrenValues (form.Controls);
120 void ResetChildrenValues (ControlCollection children)
122 foreach (Control child in children) {
126 if (child.HasControls ())
127 ResetChildrenValues (child.Controls);
128 ResetChildValue (child);
132 void ResetChildValue (Control child)
134 Type type = child.GetType ();
135 object[] attributes = type.GetCustomAttributes (false);
136 if (attributes == null || attributes.Length == 0)
139 string defaultProperty = null;
140 DefaultPropertyAttribute defprop;
142 foreach (object attr in attributes) {
143 defprop = attr as DefaultPropertyAttribute;
146 defaultProperty = defprop.Name;
150 if (defaultProperty == null || defaultProperty.Length == 0)
153 PropertyInfo pi = null;
155 pi = type.GetProperty (defaultProperty,
156 BindingFlags.Instance |
157 BindingFlags.Public |
158 BindingFlags.Static |
159 BindingFlags.IgnoreCase);
160 } catch (Exception) {
163 if (pi == null || !pi.CanWrite)
166 attributes = pi.GetCustomAttributes (false);
167 if (attributes == null || attributes.Length == 0)
170 DefaultValueAttribute defval = null;
173 foreach (object attr in attributes) {
174 defval = attr as DefaultValueAttribute;
177 value = defval.Value;
181 if (value == null || pi.PropertyType != value.GetType ())
185 pi.SetValue (child, value, null);
186 } catch (Exception) {
191 protected virtual void RaisePostBackEvent (string eventArgument)
193 RaisePostBackEventInternal (eventArgument);
196 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
198 RaisePostBackEvent (eventArgument);
201 protected internal override void OnPreRender (EventArgs e)
203 base.OnPreRender (e);
204 if (Events [ServerClickEvent] != null)
205 Page.RequiresPostBackScript ();
208 protected virtual void OnServerClick (EventArgs e)
210 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
211 if (server_click != null)
212 server_click (this, e);
215 protected override void RenderAttributes (HtmlTextWriter writer)
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))) {
223 string onclick = String.Empty;
224 if (Attributes ["onclick"] != null) {
225 onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
226 Attributes.Remove ("onclick");
231 PostBackOptions options = GetPostBackOptions ();
232 onclick += page.ClientScript.GetPostBackEventReference (options, true);
235 if (onclick.Length > 0) {
236 writer.WriteAttribute ("onclick", onclick, true);
237 writer.WriteAttribute ("language", "javascript");
241 Attributes.Remove ("CausesValidation");
242 // LAMESPEC: MS doesn't actually remove this
243 //attribute. it shows up in the rendered
246 // Attributes.Remove("ValidationGroup");
247 base.RenderAttributes (writer);
250 PostBackOptions GetPostBackOptions ()
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;
266 [WebSysDescription("")]
267 [WebCategory("Action")]
268 public event EventHandler ServerClick {
269 add { Events.AddHandler (ServerClickEvent, value); }
270 remove { Events.RemoveHandler (ServerClickEvent, value); }