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;
34 namespace System.Web.UI.HtmlControls {
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [DefaultEventAttribute ("ServerClick")]
42 [SupportsEventValidation]
44 public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
46 private static readonly object ServerClickEvent = new object();
48 public HtmlInputButton () : this ("button")
52 public HtmlInputButton (string type) : base (type)
57 [WebSysDescription("")]
58 [WebCategory("Behavior")]
64 bool CausesValidation {
66 string flag = Attributes["CausesValidation"];
71 return Boolean.Parse (flag);
74 Attributes ["CausesValidation"] = value.ToString();
80 public virtual string ValidationGroup
83 string group = Attributes["ValidationGroup"];
92 Attributes.Remove ("ValidationGroup");
94 Attributes["ValidationGroup"] = value;
99 void RaisePostBackEventInternal (string eventArgument)
101 if (CausesValidation) {
103 Page.Validate (ValidationGroup);
109 if (String.Compare (Type, "reset", true, CultureInfo.InvariantCulture) != 0)
110 OnServerClick (EventArgs.Empty);
112 ResetForm (FindForm ());
135 void ResetForm (HtmlForm form)
137 if (form == null || !form.HasControls ())
140 ResetChildrenValues (form.Controls);
143 void ResetChildrenValues (ControlCollection children)
145 foreach (Control child in children) {
149 if (child.HasControls ())
150 ResetChildrenValues (child.Controls);
151 ResetChildValue (child);
155 void ResetChildValue (Control child)
157 Type type = child.GetType ();
158 object[] attributes = type.GetCustomAttributes (false);
159 if (attributes == null || attributes.Length == 0)
162 string defaultProperty = null;
163 DefaultPropertyAttribute defprop;
165 foreach (object attr in attributes) {
166 defprop = attr as DefaultPropertyAttribute;
169 defaultProperty = defprop.Name;
173 if (defaultProperty == null || defaultProperty.Length == 0)
176 PropertyInfo pi = null;
178 pi = type.GetProperty (defaultProperty,
179 BindingFlags.Instance |
180 BindingFlags.Public |
181 BindingFlags.Static |
182 BindingFlags.IgnoreCase);
183 } catch (Exception) {
186 if (pi == null || !pi.CanWrite)
189 attributes = pi.GetCustomAttributes (false);
190 if (attributes == null || attributes.Length == 0)
193 DefaultValueAttribute defval = null;
196 foreach (object attr in attributes) {
197 defval = attr as DefaultValueAttribute;
200 value = defval.Value;
204 if (value == null || pi.PropertyType != value.GetType ())
208 pi.SetValue (child, value, null);
209 } catch (Exception) {
214 protected virtual void RaisePostBackEvent (string eventArgument)
216 RaisePostBackEventInternal (eventArgument);
220 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
223 RaisePostBackEvent (eventArgument);
225 RaisePostBackEventInternal (eventArgument);
234 override void OnPreRender (EventArgs e)
236 base.OnPreRender (e);
237 if (Events [ServerClickEvent] != null)
238 Page.RequiresPostBackScript ();
241 protected virtual void OnServerClick (EventArgs e)
243 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
244 if (server_click != null)
245 server_click (this, e);
249 bool RenderOnClick ()
251 if (Page == null || !CausesValidation)
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)
260 if (0 == String.Compare (input_type, "button", true, inv) &&
261 Events [ServerClickEvent] != null)
268 protected override void RenderAttributes (HtmlTextWriter writer)
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))) {
277 string onclick = String.Empty;
278 if (Attributes ["onclick"] != null) {
279 onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
280 Attributes.Remove ("onclick");
283 PostBackOptions options = GetPostBackOptions ();
284 Page.ClientScript.RegisterForEventValidation (options);
285 onclick += Page.ClientScript.GetPostBackEventReference (options);
288 if (onclick.Length > 0) {
289 writer.WriteAttribute ("onclick", onclick, true);
290 writer.WriteAttribute ("language", "javascript");
294 if (RenderOnClick ()) {
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, "");
304 writer.WriteAttribute ("language", "javascript");
305 writer.WriteAttribute ("onclick", oc, true);
310 Attributes.Remove ("CausesValidation");
312 // LAMESPEC: MS doesn't actually remove this
313 //attribute. it shows up in the rendered
316 // Attributes.Remove("ValidationGroup");
318 base.RenderAttributes (writer);
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;
337 [WebSysDescription("")]
338 [WebCategory("Action")]
339 public event EventHandler ServerClick {
340 add { Events.AddHandler (ServerClickEvent, value); }
341 remove { Events.RemoveHandler (ServerClickEvent, value); }