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 Page.ClientScript.RegisterForEventValidation (options);
125 Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options);
126 writer.WriteAttribute ("language", "javascript");
129 bool postback = false;
131 if (Page != null && Events [ServerClickEvent] != null)
134 if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
136 writer.WriteAttribute ("onclick",
137 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0}}}",
138 csm.GetPostBackEventReference (this, String.Empty)));
140 writer.WriteAttribute ("onclick",
141 "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
143 writer.WriteAttribute ("language", "javascript");
146 writer.WriteAttribute ("onclick",
147 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
149 writer.WriteAttribute ("language", "javascript");
153 base.RenderAttributes (writer);
157 PostBackOptions GetPostBackOptions () {
158 PostBackOptions options = new PostBackOptions (this);
159 options.ValidationGroup = null;
160 options.ActionUrl = null;
161 options.Argument = "";
162 options.RequiresJavaScriptProtocol = false;
163 options.ClientSubmit = true;
164 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
165 if (options.PerformValidation)
166 options.ValidationGroup = ValidationGroup;
172 [WebSysDescription("")]
173 [WebCategory("Action")]
174 public event EventHandler ServerClick {
175 add { Events.AddHandler (ServerClickEvent, value); }
176 remove { Events.RemoveHandler (ServerClickEvent, value); }