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 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);
121 bool postback = false;
123 if (Page != null && Events [ServerClickEvent] != null)
126 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
128 writer.WriteAttribute ("onclick",
129 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0}}}",
130 csm.GetPostBackEventReference (this, String.Empty)));
132 writer.WriteAttribute ("onclick",
133 "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
135 writer.WriteAttribute ("language", "javascript");
138 writer.WriteAttribute ("onclick",
139 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
141 writer.WriteAttribute ("language", "javascript");
144 base.RenderAttributes (writer);
147 [WebSysDescription("")]
148 [WebCategory("Action")]
149 public event EventHandler ServerClick {
150 add { Events.AddHandler (ServerClickEvent, value); }
151 remove { Events.RemoveHandler (ServerClickEvent, value); }