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 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)
93 ValidateEvent (UniqueID, eventArgument);
97 Page.Validate (ValidationGroup);
101 OnServerClick (EventArgs.Empty);
109 override void OnPreRender (EventArgs e)
111 base.OnPreRender (e);
114 protected virtual void OnServerClick (EventArgs e)
116 EventHandler server_click = (EventHandler) Events [ServerClickEvent];
117 if (server_click != null)
118 server_click (this, e);
121 protected override void RenderAttributes (HtmlTextWriter writer)
124 if (Page != null && Events [ServerClickEvent] != null) {
125 PostBackOptions options = GetPostBackOptions ();
126 Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options, true);
127 writer.WriteAttribute ("language", "javascript");
130 ClientScriptManager csm = new ClientScriptManager (Page);
131 bool postback = false;
133 if (Page != null && Events [ServerClickEvent] != null)
136 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
138 writer.WriteAttribute ("onclick",
139 String.Concat ("javascript:{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) ",
140 csm.GetPostBackEventReference (this, String.Empty), "}"));
142 writer.WriteAttribute ("onclick",
143 "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
145 writer.WriteAttribute ("language", "javascript");
148 writer.WriteAttribute ("onclick",
149 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
151 writer.WriteAttribute ("language", "javascript");
155 base.RenderAttributes (writer);
159 PostBackOptions GetPostBackOptions () {
160 PostBackOptions options = new PostBackOptions (this);
161 options.ValidationGroup = null;
162 options.ActionUrl = null;
163 options.Argument = String.Empty;
164 options.RequiresJavaScriptProtocol = false;
165 options.ClientSubmit = true;
166 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
167 if (options.PerformValidation)
168 options.ValidationGroup = ValidationGroup;
174 [WebSysDescription("")]
175 [WebCategory("Action")]
176 public event EventHandler ServerClick {
177 add { Events.AddHandler (ServerClickEvent, value); }
178 remove { Events.RemoveHandler (ServerClickEvent, value); }