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")]
40 public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
42 private static readonly object ServerClickEvent = new object();
44 public HtmlButton () : base ("button")
49 [WebSysDescription("")]
50 [WebCategory("Behavior")]
56 bool CausesValidation {
58 return ViewState.GetBool ("CausesValidation", true);
61 ViewState ["CausesValidation"] = value;
67 public string ValidationGroup
70 return ViewState.GetString ("ValidationGroup", "");
73 ViewState ["ValidationGroup"] = value;
79 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
81 RaisePostBackEvent (eventArgument);
84 protected virtual void RaisePostBackEvent (string eventArgument)
86 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
91 Page.Validate (ValidationGroup);
95 OnServerClick (EventArgs.Empty);
103 override void OnPreRender (EventArgs e)
105 base.OnPreRender (e);
108 protected virtual void OnServerClick (EventArgs e)
110 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
111 if (server_click != null)
112 server_click (this, e);
115 protected override void RenderAttributes (HtmlTextWriter writer)
117 ClientScriptManager csm = new ClientScriptManager (Page);
118 bool postback = false;
120 if (Page != null && Events [ServerClickEvent] != null)
123 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
125 writer.WriteAttribute ("onclick",
126 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0}}}",
127 csm.GetPostBackEventReference (this, String.Empty)));
129 writer.WriteAttribute ("onclick",
130 "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
132 writer.WriteAttribute ("language", "javascript");
135 writer.AddAttribute ("onclick",
136 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
138 writer.WriteAttribute ("language", "javascript");
141 base.RenderAttributes (writer);
144 [WebSysDescription("")]
145 [WebCategory("Action")]
146 public event EventHandler ServerClick {
147 add { Events.AddHandler (ServerClickEvent, value); }
148 remove { Events.RemoveHandler (ServerClickEvent, value); }