2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // System.Web.UI.HtmlControls.HtmlButton
25 // Jackson Harper (jackson@ximian.com)
27 // (C) 2005 Novell, Inc.
30 using System.ComponentModel;
31 using System.Security.Permissions;
33 namespace System.Web.UI.HtmlControls {
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [DefaultEvent("ServerClick")]
41 [SupportsEventValidation]
43 public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
45 private static readonly object ServerClickEvent = new object();
47 public HtmlButton () : base ("button")
52 [WebSysDescription("")]
53 [WebCategory("Behavior")]
59 bool CausesValidation {
61 return ViewState.GetBool ("CausesValidation", true);
64 ViewState ["CausesValidation"] = value;
70 public virtual string ValidationGroup
73 return ViewState.GetString ("ValidationGroup", "");
76 ViewState ["ValidationGroup"] = value;
82 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
84 RaisePostBackEvent (eventArgument);
87 protected virtual void RaisePostBackEvent (string eventArgument)
89 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
94 Page.Validate (ValidationGroup);
98 OnServerClick (EventArgs.Empty);
106 override void OnPreRender (EventArgs e)
108 base.OnPreRender (e);
111 protected virtual void OnServerClick (EventArgs e)
113 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
114 if (server_click != null)
115 server_click (this, e);
118 protected override void RenderAttributes (HtmlTextWriter writer)
120 ClientScriptManager csm = new ClientScriptManager (Page);
122 if (Page != null && Events [ServerClickEvent] != null) {
123 PostBackOptions options = GetPostBackOptions ();
124 Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options);
125 writer.WriteAttribute ("language", "javascript");
128 bool postback = false;
130 if (Page != null && Events [ServerClickEvent] != null)
133 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
135 writer.WriteAttribute ("onclick",
136 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0}}}",
137 csm.GetPostBackEventReference (this, String.Empty)));
139 writer.WriteAttribute ("onclick",
140 "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
142 writer.WriteAttribute ("language", "javascript");
145 writer.WriteAttribute ("onclick",
146 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
148 writer.WriteAttribute ("language", "javascript");
152 base.RenderAttributes (writer);
156 PostBackOptions GetPostBackOptions () {
157 PostBackOptions options = new PostBackOptions (this);
158 options.ValidationGroup = null;
159 options.ActionUrl = null;
160 options.Argument = "";
161 options.RequiresJavaScriptProtocol = false;
162 options.ClientSubmit = true;
163 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
164 if (options.PerformValidation)
165 options.ValidationGroup = ValidationGroup;
171 [WebSysDescription("")]
172 [WebCategory("Action")]
173 public event EventHandler ServerClick {
174 add { Events.AddHandler (ServerClickEvent, value); }
175 remove { Events.RemoveHandler (ServerClickEvent, value); }