1 //
2 // System.Web.UI.WebControls.Button.cs
3 //
4 // Authors:
5 //	Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28 
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.Security.Permissions;
32 
33 namespace System.Web.UI.WebControls
34 {
35 	// CAS
36 	[AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 	[AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 	// attributes
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
46 	{
47 		static readonly object ClickEvent = new object ();
48 		static readonly object CommandEvent = new object ();
49 
Button()50 		public Button () : base (HtmlTextWriterTag.Input)
51 		{
52 		}
53 
54 		[WebSysDescription ("")]
55 		[WebCategory ("Behavior")]
56 		[DefaultValue (true)]
57 		[Themeable (false)]
58 		public virtual bool CausesValidation {
59 			get { return ViewState.GetBool ("CausesValidation", true); }
60 			set { ViewState ["CausesValidation"] = value; }
61 		}
62 
63 		[DefaultValue ("")]
64 		[Bindable (true)]
65 		[WebSysDescription ("")]
66 		[WebCategory ("Behavior")]
67 		[Themeable (false)]
68 		public string CommandArgument {
69 			get { return ViewState.GetString ("CommandArgument", String.Empty); }
70 			set { ViewState ["CommandArgument"] = value; }
71 		}
72 
73 		[DefaultValue ("")]
74 		[WebSysDescription ("")]
75 		[WebCategory ("Behavior")]
76 		[Themeable (false)]
77 		public string CommandName {
78 			get { return ViewState.GetString ("CommandName", String.Empty); }
79 			set { ViewState ["CommandName"] = value; }
80 		}
81 
82 		[Themeable (false)]
83 		[DefaultValue ("")]
84 		[WebSysDescription ("")]
85 		[WebCategoryAttribute ("Behavior")]
86 		public virtual string OnClientClick {
87 			get { return ViewState.GetString ("OnClientClick", String.Empty); }
88 			set { ViewState ["OnClientClick"] = value; }
89 		}
90 
91 		[DefaultValue ("")]
92 		[Bindable (true)]
93 		[WebSysDescription ("")]
94 		[WebCategory ("Appearance")]
95 		[Localizable (true)]
96 		public string Text {
97 			get { return ViewState.GetString ("Text", String.Empty); }
98 			set { ViewState ["Text"] = value; }
99 		}
100 
101 		[DefaultValue (true)]
102 		[Themeable (false)]
103 		[WebSysDescription ("")]
104 		[WebCategoryAttribute ("Behavior")]
105 		public virtual bool UseSubmitBehavior  {
106 			get { return ViewState.GetBool ("UseSubmitBehavior", true); }
107 			set { ViewState ["UseSubmitBehavior"] = value; }
108 		}
109 
AddAttributesToRender(HtmlTextWriter writer)110 		protected override void AddAttributesToRender (HtmlTextWriter writer)
111 		{
112 			Page page = Page;
113 			if (page != null)
114 				page.VerifyRenderingInServerForm (this);
115 
116 			writer.AddAttribute (HtmlTextWriterAttribute.Type, UseSubmitBehavior ? "submit" : "button", false);
117 			writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
118 			writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
119 
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");
125 			}
126 
127 			if (page != null)
128 				onclick += GetClientScriptEventReference ();
129 
130 			if (onclick.Length > 0)
131 				writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
132 
133 			base.AddAttributesToRender (writer);
134 		}
135 
GetClientScriptEventReference()136 		internal virtual string GetClientScriptEventReference ()
137 		{
138 			PostBackOptions options = GetPostBackOptions ();
139 			Page page = Page;
140 			if (page != null)
141 				return page.ClientScript.GetPostBackEventReference (options, true);
142 			else
143 				return String.Empty;
144 		}
145 
GetPostBackOptions()146 		protected virtual PostBackOptions GetPostBackOptions ()
147 		{
148 			PostBackOptions options = new PostBackOptions (this);
149 			options.ActionUrl = (PostBackUrl.Length > 0 ?
150 				Page.ResolveClientUrl (PostBackUrl)
151 				: null);
152 			options.ValidationGroup = null;
153 			options.Argument = String.Empty;
154 			options.RequiresJavaScriptProtocol = false;
155 			options.ClientSubmit = !UseSubmitBehavior;
156 
157 			Page page = Page;
158 			options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
159 			if (options.PerformValidation)
160 				options.ValidationGroup = ValidationGroup;
161 
162 			return options;
163 		}
164 
IPostBackEventHandler.RaisePostBackEvent(string eventArgument)165 		void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
166 		{
167 			RaisePostBackEvent (eventArgument);
168 		}
169 
OnClick(EventArgs e)170 		protected virtual void OnClick (EventArgs e)
171 		{
172 			if (Events != null) {
173 				EventHandler eh = (EventHandler) (Events [ClickEvent]);
174 				if (eh != null)
175 					eh (this, e);
176 			}
177 		}
178 
OnCommand(CommandEventArgs e)179 		protected virtual void OnCommand (CommandEventArgs e)
180 		{
181 			if (Events != null) {
182 				CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
183 				if (eh != null)
184 					eh (this, e);
185 			}
186 
187 			RaiseBubbleEvent (this, e);
188 		}
189 
RaisePostBackEvent(string eventArgument)190 		protected virtual void RaisePostBackEvent (string eventArgument)
191 		{
192 			ValidateEvent (UniqueID, eventArgument);
193 			if (CausesValidation) {
194 				Page page = Page;
195 				if (page != null)
196 					page.Validate (ValidationGroup);
197 			}
198 
199 			OnClick (EventArgs.Empty);
200 			OnCommand (new CommandEventArgs (CommandName, CommandArgument));
201 		}
202 
OnPreRender(EventArgs e)203 		protected internal override void OnPreRender (EventArgs e)
204 		{
205 			// Why override?
206 			base.OnPreRender (e);
207 		}
208 
RenderContents(HtmlTextWriter writer)209 		protected internal override void RenderContents (HtmlTextWriter writer)
210                 {
211                 }
212 
213 		[WebSysDescription ("")]
214 		[WebCategory ("Action")]
215 		public event EventHandler Click {
216 			add { Events.AddHandler (ClickEvent, value); }
217 			remove { Events.RemoveHandler (ClickEvent, value); }
218 		}
219 
220 		[WebSysDescription ("")]
221 		[WebCategory ("Action")]
222 		public event CommandEventHandler Command {
223 			add { Events.AddHandler (CommandEvent, value); }
224 			remove { Events.RemoveHandler (CommandEvent, value); }
225 		}
226 
227 		[DefaultValue ("")]
228 		[Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
229 		[Themeable (false)]
230 		[UrlProperty("*.aspx")]
231 		public virtual string PostBackUrl {
232 			get { return ViewState.GetString ("PostBackUrl", String.Empty); }
233 			set { ViewState ["PostBackUrl"] = value; }
234 		}
235 
236 		[DefaultValue ("")]
237 		[Themeable (false)]
238 		[WebSysDescription ("")]
239 		[WebCategoryAttribute ("Behavior")]
240 		public virtual string ValidationGroup {
241 			get { return ViewState.GetString ("ValidationGroup", String.Empty); }
242 			set { ViewState ["ValidationGroup"] = value; }
243 		}
244 	}
245 }
246