2 // System.Web.UI.WebControls.Button.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
39 namespace System.Web.UI.WebControls
41 [DefaultEvent("Click")]
42 [DefaultProperty("Text")]
43 [Designer("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
44 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
45 [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
46 public class Button : WebControl, IPostBackEventHandler
48 private static readonly object ClickEvent = new object();
49 private static readonly object CommandEvent = new object();
51 //private EventHandlerList ehList;
53 public Button(): base (HtmlTextWriterTag.Input)
57 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
58 [WebSysDescription ("Determines if validation is performed when clicked.")]
59 public bool CausesValidation
63 Object cv = ViewState["CausesValidation"];
70 ViewState["CausesValidation"] = value;
74 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
75 [WebSysDescription ("An argument for the Command of this control.")]
76 public string CommandArgument
80 string ca = (string) ViewState["CommandArgument"];
87 ViewState["CommandArgument"] = value;
91 [DefaultValue (""), WebCategory ("Behavior")]
92 [WebSysDescription ("The name of the Command of this control.")]
93 public string CommandName
97 string cn = (string)ViewState["CommandName"];
104 ViewState["CommandName"] = value;
108 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
109 [WebSysDescription ("The text that should be shown on this Button.")]
114 string text = (string)ViewState["Text"];
121 ViewState["Text"] = value;
125 [WebCategory ("Action")]
126 [WebSysDescription ("Raised when the Button is clicked.")]
127 public event EventHandler Click
131 Events.AddHandler(ClickEvent, value);
135 Events.RemoveHandler(ClickEvent, value);
139 [WebCategory ("Action")]
140 [WebSysDescription ("Raised when a Button Command is executed.")]
141 public event CommandEventHandler Command
145 Events.AddHandler(CommandEvent, value);
149 Events.RemoveHandler(CommandEvent, value);
153 protected virtual void OnClick(EventArgs e)
157 EventHandler eh = (EventHandler)(Events[ClickEvent]);
163 protected virtual void OnCommand(CommandEventArgs e)
167 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
171 RaiseBubbleEvent(this, e);
174 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
176 if (CausesValidation)
179 OnClick (EventArgs.Empty);
180 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
183 protected override void AddAttributesToRender (HtmlTextWriter writer)
186 Page.VerifyRenderingInServerForm (this);
188 writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
189 writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
190 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
191 if (Page != null && CausesValidation && Page.Validators.Count > 0) {
192 writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick,
193 Utils.GetClientValidatedEvent (Page));
194 writer.AddAttribute ("language", "javascript");
196 base.AddAttributesToRender (writer);
199 protected override void RenderContents(HtmlTextWriter writer)
201 // Preventing base classes to do anything