2 // System.Web.UI.WebControls.Button.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005-2010 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")]
43 [ToolboxDataAttribute ("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
44 [SupportsEventValidation]
45 public class Button : WebControl, IPostBackEventHandler, IButtonControl
47 static readonly object ClickEvent = new object ();
48 static readonly object CommandEvent = new object ();
50 public Button () : base (HtmlTextWriterTag.Input)
54 [WebSysDescription ("")]
55 [WebCategory ("Behavior")]
58 public virtual bool CausesValidation {
59 get { return ViewState.GetBool ("CausesValidation", true); }
60 set { ViewState ["CausesValidation"] = value; }
65 [WebSysDescription ("")]
66 [WebCategory ("Behavior")]
68 public string CommandArgument {
69 get { return ViewState.GetString ("CommandArgument", String.Empty); }
70 set { ViewState ["CommandArgument"] = value; }
74 [WebSysDescription ("")]
75 [WebCategory ("Behavior")]
77 public string CommandName {
78 get { return ViewState.GetString ("CommandName", String.Empty); }
79 set { ViewState ["CommandName"] = value; }
84 [WebSysDescription ("")]
85 [WebCategoryAttribute ("Behavior")]
86 public virtual string OnClientClick {
87 get { return ViewState.GetString ("OnClientClick", String.Empty); }
88 set { ViewState ["OnClientClick"] = value; }
93 [WebSysDescription ("")]
94 [WebCategory ("Appearance")]
97 get { return ViewState.GetString ("Text", String.Empty); }
98 set { ViewState ["Text"] = value; }
101 [DefaultValue (true)]
103 [WebSysDescription ("")]
104 [WebCategoryAttribute ("Behavior")]
105 public virtual bool UseSubmitBehavior {
106 get { return ViewState.GetBool ("UseSubmitBehavior", true); }
107 set { ViewState ["UseSubmitBehavior"] = value; }
110 protected override void AddAttributesToRender (HtmlTextWriter writer)
114 page.VerifyRenderingInServerForm (this);
116 writer.AddAttribute (HtmlTextWriterAttribute.Type, UseSubmitBehavior ? "submit" : "button", false);
117 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
118 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
120 string onclick = OnClientClick;
121 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
122 if (HasAttributes && Attributes ["onclick"] != null) {
123 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
124 Attributes.Remove ("onclick");
128 onclick += GetClientScriptEventReference ();
130 if (onclick.Length > 0)
131 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
133 base.AddAttributesToRender (writer);
136 internal virtual string GetClientScriptEventReference ()
138 PostBackOptions options = GetPostBackOptions ();
141 return page.ClientScript.GetPostBackEventReference (options, true);
146 protected virtual PostBackOptions GetPostBackOptions ()
148 PostBackOptions options = new PostBackOptions (this);
149 options.ActionUrl = (PostBackUrl.Length > 0 ?
150 Page.ResolveClientUrl (PostBackUrl)
152 options.ValidationGroup = null;
153 options.Argument = String.Empty;
154 options.RequiresJavaScriptProtocol = false;
155 options.ClientSubmit = !UseSubmitBehavior;
158 options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
159 if (options.PerformValidation)
160 options.ValidationGroup = ValidationGroup;
165 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
167 RaisePostBackEvent (eventArgument);
170 protected virtual void OnClick (EventArgs e)
172 if (Events != null) {
173 EventHandler eh = (EventHandler) (Events [ClickEvent]);
179 protected virtual void OnCommand (CommandEventArgs e)
181 if (Events != null) {
182 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
187 RaiseBubbleEvent (this, e);
190 protected virtual void RaisePostBackEvent (string eventArgument)
192 ValidateEvent (UniqueID, eventArgument);
193 if (CausesValidation) {
196 page.Validate (ValidationGroup);
199 OnClick (EventArgs.Empty);
200 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
203 protected internal override void OnPreRender (EventArgs e)
206 base.OnPreRender (e);
209 protected internal override void RenderContents (HtmlTextWriter writer)
213 [WebSysDescription ("")]
214 [WebCategory ("Action")]
215 public event EventHandler Click {
216 add { Events.AddHandler (ClickEvent, value); }
217 remove { Events.RemoveHandler (ClickEvent, value); }
220 [WebSysDescription ("")]
221 [WebCategory ("Action")]
222 public event CommandEventHandler Command {
223 add { Events.AddHandler (CommandEvent, value); }
224 remove { Events.RemoveHandler (CommandEvent, value); }
228 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
230 [UrlProperty("*.aspx")]
231 public virtual string PostBackUrl {
232 get { return ViewState.GetString ("PostBackUrl", String.Empty); }
233 set { ViewState ["PostBackUrl"] = value; }
238 [WebSysDescription ("")]
239 [WebCategoryAttribute ("Behavior")]
240 public virtual string ValidationGroup {
241 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
242 set { ViewState ["ValidationGroup"] = value; }