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 virtual 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);
145 bool RenderOnClick ()
147 if (Page == null || !CausesValidation)
150 CultureInfo inv = CultureInfo.InvariantCulture;
151 string input_type = Type;
152 if (0 == String.Compare (input_type, "submit", true, inv) &&
153 Page.Validators.Count > 0)
156 if (0 == String.Compare (input_type, "button", true, inv) &&
157 Events [ServerClickEvent] != null)
164 protected override void RenderAttributes (HtmlTextWriter writer)
167 CultureInfo inv = CultureInfo.InvariantCulture;
168 string input_type = Type;
169 if (0 != String.Compare (input_type, "reset", true, inv) &&
170 ((0 == String.Compare (input_type, "submit", true, inv)) ||
171 (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
173 string onclick = String.Empty;
174 if (Attributes ["onclick"] != null) {
175 onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
176 Attributes.Remove ("onclick");
179 PostBackOptions options = GetPostBackOptions ();
180 Page.ClientScript.RegisterForEventValidation (options);
181 onclick += Page.ClientScript.GetPostBackEventReference (options);
184 if (onclick.Length > 0) {
185 writer.WriteAttribute ("onclick", onclick);
186 writer.WriteAttribute ("language", "javascript");
190 if (RenderOnClick ()) {
192 ClientScriptManager csm = new ClientScriptManager (Page);
193 if (Page.AreValidatorsUplevel ()) {
194 oc = csm.GetClientValidationEvent ();
195 } else if (Events [ServerClickEvent] != null) {
196 oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
200 writer.WriteAttribute ("language", "javascript");
201 writer.WriteAttribute ("onclick", oc);
206 Attributes.Remove ("CausesValidation");
208 // LAMESPEC: MS doesn't actually remove this
209 //attribute. it shows up in the rendered
212 // Attributes.Remove("ValidationGroup");
214 base.RenderAttributes (writer);
218 PostBackOptions GetPostBackOptions () {
219 PostBackOptions options = new PostBackOptions (this);
220 options.ValidationGroup = null;
221 options.ActionUrl = null;
222 options.Argument = "";
223 options.RequiresJavaScriptProtocol = false;
224 options.ClientSubmit = (0 != String.Compare (Type, "submit", true, CultureInfo.InvariantCulture));
225 options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
226 if (options.PerformValidation)
227 options.ValidationGroup = ValidationGroup;
233 [WebSysDescription("")]
234 [WebCategory("Action")]
235 public event EventHandler ServerClick {
236 add { Events.AddHandler (ServerClickEvent, value); }
237 remove { Events.RemoveHandler (ServerClickEvent, value); }