**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / Button.cs
bloba6ad66ab4119b84d315bc96200c90c616592f59f
1 //
2 // System.Web.UI.WebControls.Button.cs
3 //
4 // Authors:
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36 using System.Web;
37 using System.Web.UI;
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))]
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
61 get
63 Object cv = ViewState["CausesValidation"];
64 if(cv!=null)
65 return (Boolean)cv;
66 return true;
68 set
70 ViewState["CausesValidation"] = value;
74 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
75 [WebSysDescription ("An argument for the Command of this control.")]
76 public string CommandArgument
78 get
80 string ca = (string) ViewState["CommandArgument"];
81 if(ca!=null)
82 return ca;
83 return String.Empty;
85 set
87 ViewState["CommandArgument"] = value;
91 [DefaultValue (""), WebCategory ("Behavior")]
92 [WebSysDescription ("The name of the Command of this control.")]
93 public string CommandName
95 get
97 string cn = (string)ViewState["CommandName"];
98 if(cn!=null)
99 return cn;
100 return String.Empty;
104 ViewState["CommandName"] = value;
108 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
109 [WebSysDescription ("The text that should be shown on this Button.")]
110 public string Text
114 string text = (string)ViewState["Text"];
115 if(text!=null)
116 return text;
117 return String.Empty;
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);
133 remove
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);
147 remove
149 Events.RemoveHandler(CommandEvent, value);
153 protected virtual void OnClick(EventArgs e)
155 if(Events != null)
157 EventHandler eh = (EventHandler)(Events[ClickEvent]);
158 if(eh!= null)
159 eh(this,e);
163 protected virtual void OnCommand(CommandEventArgs e)
165 if(Events != null)
167 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
168 if(eh!= null)
169 eh(this,e);
171 RaiseBubbleEvent(this, e);
174 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
176 if (CausesValidation)
177 Page.Validate ();
179 OnClick (EventArgs.Empty);
180 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
183 protected override void AddAttributesToRender (HtmlTextWriter writer)
185 if (Page != null)
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