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
51 private static readonly object ClickEvent = new object();
52 private static readonly object CommandEvent = new object();
54 //private EventHandlerList ehList;
56 public Button(): base (HtmlTextWriterTag.Input)
61 [ThemeableAttribute (false)]
65 [DefaultValue (true), WebCategory ("Behavior")]
66 [WebSysDescription ("Determines if validation is performed when clicked.")]
67 public bool CausesValidation
71 Object cv = ViewState["CausesValidation"];
78 ViewState["CausesValidation"] = value;
83 [ThemeableAttribute (false)]
85 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
86 [WebSysDescription ("An argument for the Command of this control.")]
87 public string CommandArgument
91 string ca = (string) ViewState["CommandArgument"];
98 ViewState["CommandArgument"] = value;
103 [ThemeableAttribute (false)]
105 [DefaultValue (""), WebCategory ("Behavior")]
106 [WebSysDescription ("The name of the Command of this control.")]
107 public string CommandName
111 string cn = (string)ViewState["CommandName"];
118 ViewState["CommandName"] = value;
125 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
126 [WebSysDescription ("The text that should be shown on this Button.")]
131 string text = (string)ViewState["Text"];
138 ViewState["Text"] = value;
143 [ThemeableAttribute (false)]
144 [EditorAttribute ("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
145 [DefaultValueAttribute ("")]
147 public string PostBackUrl {
149 string text = (string)ViewState["PostBackUrl"];
150 if (text!=null) return text;
154 ViewState["PostBackUrl"] = value;
158 [BindableAttribute (true)]
159 [LocalizableAttribute (true)]
160 [DefaultValueAttribute ("")]
161 public string SoftkeyLabel {
163 string text = (string)ViewState["SoftkeyLabel"];
164 if (text!=null) return text;
168 ViewState["SoftkeyLabel"] = value;
172 [DefaultValueAttribute ("")]
173 [ThemeableAttribute (false)]
174 [WebCategoryAttribute ("Behavior")]
175 public string ValidationGroup {
177 string text = (string)ViewState["ValidationGroup"];
178 if (text!=null) return text;
182 ViewState["ValidationGroup"] = value;
186 [DefaultValueAttribute ("")]
187 [ThemeableAttribute (false)]
188 [WebCategoryAttribute ("Behavior")]
189 public string OnClientClick {
191 string text = (string)ViewState["OnClientClick"];
192 if (text!=null) return text;
196 ViewState["OnClientClick"] = value;
200 [DefaultValueAttribute (true)]
201 [ThemeableAttribute (false)]
202 [WebCategoryAttribute ("Behavior")]
203 public bool UseSubmitBehavior {
205 object b = ViewState["UseSubmitBehavior"];
206 if (b != null) return (bool)b;
210 ViewState["UseSubmitBehavior"] = value;
215 [WebCategory ("Action")]
216 [WebSysDescription ("Raised when the Button is clicked.")]
217 public event EventHandler Click
221 Events.AddHandler(ClickEvent, value);
225 Events.RemoveHandler(ClickEvent, value);
229 [WebCategory ("Action")]
230 [WebSysDescription ("Raised when a Button Command is executed.")]
231 public event CommandEventHandler Command
235 Events.AddHandler(CommandEvent, value);
239 Events.RemoveHandler(CommandEvent, value);
243 protected virtual void OnClick(EventArgs e)
247 EventHandler eh = (EventHandler)(Events[ClickEvent]);
253 protected virtual void OnCommand(CommandEventArgs e)
257 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
261 RaiseBubbleEvent(this, e);
265 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
267 RaisePostBackEvent (eventArgument);
270 protected virtual void RaisePostBackEvent (string eventArgument)
272 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
275 if (CausesValidation)
278 OnClick (EventArgs.Empty);
279 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
283 protected virtual PostBackOptions GetPostBackOptions ()
285 PostBackOptions ops = new PostBackOptions (this);
286 if (PostBackUrl != "")
287 ops.ActionUrl = PostBackUrl;
288 ops.PerformValidation = Page.Validators.Count > 0 && CausesValidation;
289 if (ops.PerformValidation)
290 ops.ValidationGroup = ValidationGroup;
291 ops.ClientSubmit = UseSubmitBehavior;
292 ops.RequiresJavaScriptProtocol = false;
297 protected override void AddAttributesToRender (HtmlTextWriter writer)
300 Page.VerifyRenderingInServerForm (this);
302 writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
303 writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
304 writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
309 script = OnClientClick;
310 if (script.Length > 0) script += ";";
312 PostBackOptions ops = GetPostBackOptions ();
313 if (ops != null && ops.RequiresSpecialPostBack) {
314 script += Page.GetPostBackEventReference (ops);
316 else if (CausesValidation && Page.Validators.Count > 0) {
317 if (UseSubmitBehavior)
318 script += Utils.GetClientValidatedEvent (Page);
320 script += Utils.GetClientValidatedPostBack (this);
322 else if (!UseSubmitBehavior) {
323 script += Page.GetPostBackClientEvent (this,"");
326 if (CausesValidation && Page.Validators.Count > 0) {
327 script += Utils.GetClientValidatedEvent (Page);
332 writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick, script);
333 writer.AddAttribute ("language", "javascript");
337 base.AddAttributesToRender (writer);
340 protected override void RenderContents(HtmlTextWriter writer)
342 // Preventing base classes to do anything