2 // System.Web.UI.WebControls.Button.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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.ComponentModel.Design;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls {
36 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [DefaultEvent ("Click")]
40 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41 [DefaultProperty ("Text")]
42 [Designer ("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45 [ToolboxDataAttribute ("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
46 [SupportsEventValidation]
48 [ToolboxDataAttribute ("<{0}:Button runat=server Text=\"Button\"></{0}:Button>")]
51 public class Button : WebControl, IPostBackEventHandler
57 private static readonly object ClickEvent = new object ();
58 private static readonly object CommandEvent = new object ();
60 public Button () : base (HtmlTextWriterTag.Input)
67 [WebSysDescription ("")]
68 [WebCategory ("Behavior")]
76 bool CausesValidation {
78 return ViewState.GetBool ("CausesValidation", true);
82 ViewState ["CausesValidation"] = value;
88 [WebSysDescription ("")]
89 [WebCategory ("Behavior")]
93 public string CommandArgument {
95 return ViewState.GetString ("CommandArgument", "");
98 ViewState ["CommandArgument"] = value;
103 [WebSysDescription ("")]
104 [WebCategory ("Behavior")]
108 public string CommandName {
110 return ViewState.GetString ("CommandName", "");
113 ViewState ["CommandName"] = value;
120 [WebSysDescription ("")]
121 [WebCategoryAttribute ("Behavior")]
122 public virtual string OnClientClick
125 return ViewState.GetString ("OnClientClick", "");
128 ViewState ["OnClientClick"] = value;
136 [WebSysDescription ("")]
137 [WebCategory ("Appearance")]
143 return ViewState.GetString ("Text", "");
146 ViewState ["Text"] = value;
151 [DefaultValue (true)]
153 [WebSysDescription ("")]
154 [WebCategoryAttribute ("Behavior")]
155 public virtual bool UseSubmitBehavior
158 return ViewState.GetBool ("UseSubmitBehavior", true);
161 ViewState ["UseSubmitBehavior"] = value;
166 protected override void AddAttributesToRender (HtmlTextWriter writer) {
168 Page.VerifyRenderingInServerForm (this);
171 writer.AddAttribute (HtmlTextWriterAttribute.Type, UseSubmitBehavior ? "submit" : "button", false);
172 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
173 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
175 string onclick = OnClientClick;
176 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
177 if (Attributes ["onclick"] != null) {
178 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
179 Attributes.Remove ("onclick");
183 onclick += GetClientScriptEventReference ();
186 if (onclick.Length > 0)
187 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
189 writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
190 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
191 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
193 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
194 string onclick = Attributes["onclick"];
195 if (onclick != null) {
196 Attributes.Remove("onclick");
197 int len = onclick.Length;
198 if (len > 0 && onclick[len - 1] != ';')
201 ClientScriptManager csm = new ClientScriptManager (Page);
202 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
203 writer.AddAttribute ("language", "javascript");
207 base.AddAttributesToRender (writer);
211 internal virtual string GetClientScriptEventReference ()
213 PostBackOptions options = GetPostBackOptions ();
214 return Page.ClientScript.GetPostBackEventReference (options, true);
217 protected virtual PostBackOptions GetPostBackOptions ()
219 PostBackOptions options = new PostBackOptions (this);
220 options.ActionUrl = (PostBackUrl.Length > 0 ?
222 CreateActionUrl(PostBackUrl)
224 Page.ResolveClientUrl (PostBackUrl)
227 options.ValidationGroup = null;
228 options.Argument = "";
229 options.RequiresJavaScriptProtocol = false;
230 options.ClientSubmit = !UseSubmitBehavior;
231 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
232 if (options.PerformValidation)
233 options.ValidationGroup = ValidationGroup;
239 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
241 RaisePostBackEvent (eventArgument);
244 protected virtual void OnClick (EventArgs e)
246 if (Events != null) {
247 EventHandler eh = (EventHandler) (Events [ClickEvent]);
253 protected virtual void OnCommand (CommandEventArgs e)
255 if (Events != null) {
256 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
261 RaiseBubbleEvent (this, e);
267 void RaisePostBackEvent (string eventArgument)
269 if (CausesValidation)
271 Page.Validate (ValidationGroup);
276 OnClick (EventArgs.Empty);
277 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
281 protected internal override void OnPreRender (EventArgs e)
284 base.OnPreRender (e);
293 override void RenderContents (HtmlTextWriter writer)
297 [WebSysDescription ("")]
298 [WebCategory ("Action")]
299 public event EventHandler Click
302 Events.AddHandler (ClickEvent, value);
305 Events.RemoveHandler (ClickEvent, value);
309 [WebSysDescription ("")]
310 [WebCategory ("Action")]
311 public event CommandEventHandler Command
314 Events.AddHandler (CommandEvent, value);
317 Events.RemoveHandler (CommandEvent, value);
323 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
325 [UrlProperty("*.aspx")]
326 public virtual string PostBackUrl {
328 return ViewState.GetString ("PostBackUrl", "");
331 ViewState ["PostBackUrl"] = value;
337 [WebSysDescription ("")]
338 [WebCategoryAttribute ("Behavior")]
339 public virtual string ValidationGroup {
341 return ViewState.GetString ("ValidationGroup", "");
344 ViewState ["ValidationGroup"] = value;