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.Security.Permissions;
33 namespace System.Web.UI.HtmlControls {
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [DefaultEventAttribute ("ServerClick")]
41 [SupportsEventValidation]
43 public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
45 private static readonly object ServerClickEvent = new object();
47 public HtmlInputButton () : this ("button")
51 public HtmlInputButton (string type) : base (type)
56 [WebSysDescription("")]
57 [WebCategory("Behavior")]
63 bool CausesValidation {
65 string flag = Attributes["CausesValidation"];
70 return Boolean.Parse (flag);
73 Attributes ["CausesValidation"] = value.ToString();
79 public string ValidationGroup
82 string group = Attributes["ValidationGroup"];
91 Attributes.Remove ("ValidationGroup");
93 Attributes["ValidationGroup"] = value;
98 void RaisePostBackEventInternal (string eventArgument)
100 if (CausesValidation)
102 Page.Validate (ValidationGroup);
106 OnServerClick (EventArgs.Empty);
110 protected virtual void RaisePostBackEvent (string eventArgument)
112 RaisePostBackEventInternal (eventArgument);
116 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
119 RaisePostBackEvent (eventArgument);
121 RaisePostBackEventInternal (eventArgument);
130 override void OnPreRender (EventArgs e)
132 base.OnPreRender (e);
133 if (Events [ServerClickEvent] != null)
134 Page.RequiresPostBackScript ();
137 protected virtual void OnServerClick (EventArgs e)
139 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
140 if (server_click != null)
141 server_click (this, e);
144 bool RenderOnClick ()
146 if (Page == null || !CausesValidation)
149 CultureInfo inv = CultureInfo.InvariantCulture;
150 string input_type = Type;
151 if (0 == String.Compare (input_type, "submit", true, inv) &&
152 Page.Validators.Count > 0)
155 if (0 == String.Compare (input_type, "button", true, inv) &&
156 Events [ServerClickEvent] != null)
162 protected override void RenderAttributes (HtmlTextWriter writer)
164 if (RenderOnClick ()) {
166 ClientScriptManager csm = new ClientScriptManager (Page);
167 if (Page.AreValidatorsUplevel ()) {
168 oc = csm.GetClientValidationEvent ();
169 } else if (Events [ServerClickEvent] != null) {
170 oc = Attributes ["onclick"] + " " + csm.GetPostBackClientEvent (this, "");
174 writer.WriteAttribute ("language", "javascript");
175 writer.WriteAttribute ("onclick", oc);
179 Attributes.Remove ("CausesValidation");
181 // LAMESPEC: MS doesn't actually remove this
182 //attribute. it shows up in the rendered
185 // Attributes.Remove("ValidationGroup");
187 base.RenderAttributes (writer);
190 [WebSysDescription("")]
191 [WebCategory("Action")]
192 public event EventHandler ServerClick {
193 add { Events.AddHandler (ServerClickEvent, value); }
194 remove { Events.RemoveHandler (ServerClickEvent, value); }