HtmlInputButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputButton.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.Globalization;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.HtmlControls {
  32. // CAS
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. // attributes
  36. [DefaultEventAttribute ("ServerClick")]
  37. #if NET_2_0
  38. [SupportsEventValidation]
  39. #endif
  40. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
  41. private static readonly object ServerClickEvent = new object();
  42. public HtmlInputButton () : this ("button")
  43. {
  44. }
  45. public HtmlInputButton (string type) : base (type)
  46. {
  47. }
  48. [DefaultValue(true)]
  49. [WebSysDescription("")]
  50. [WebCategory("Behavior")]
  51. #if NET_2_0
  52. public virtual
  53. #else
  54. public
  55. #endif
  56. bool CausesValidation {
  57. get {
  58. string flag = Attributes["CausesValidation"];
  59. if (flag == null)
  60. return true;
  61. return Boolean.Parse (flag);
  62. }
  63. set {
  64. Attributes ["CausesValidation"] = value.ToString();
  65. }
  66. }
  67. #if NET_2_0
  68. [DefaultValue ("")]
  69. public virtual string ValidationGroup
  70. {
  71. get {
  72. string group = Attributes["ValidationGroup"];
  73. if (group == null)
  74. return "";
  75. return group;
  76. }
  77. set {
  78. if (value == null)
  79. Attributes.Remove ("ValidationGroup");
  80. else
  81. Attributes["ValidationGroup"] = value;
  82. }
  83. }
  84. #endif
  85. void RaisePostBackEventInternal (string eventArgument)
  86. {
  87. if (CausesValidation)
  88. #if NET_2_0
  89. Page.Validate (ValidationGroup);
  90. #else
  91. Page.Validate ();
  92. #endif
  93. OnServerClick (EventArgs.Empty);
  94. }
  95. #if NET_2_0
  96. protected virtual void RaisePostBackEvent (string eventArgument)
  97. {
  98. RaisePostBackEventInternal (eventArgument);
  99. }
  100. #endif
  101. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  102. {
  103. #if NET_2_0
  104. RaisePostBackEvent (eventArgument);
  105. #else
  106. RaisePostBackEventInternal (eventArgument);
  107. #endif
  108. }
  109. #if NET_2_0
  110. protected internal
  111. #else
  112. protected
  113. #endif
  114. override void OnPreRender (EventArgs e)
  115. {
  116. base.OnPreRender (e);
  117. if (Events [ServerClickEvent] != null)
  118. Page.RequiresPostBackScript ();
  119. }
  120. protected virtual void OnServerClick (EventArgs e)
  121. {
  122. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  123. if (server_click != null)
  124. server_click (this, e);
  125. }
  126. #if !NET_2_0
  127. bool RenderOnClick ()
  128. {
  129. if (Page == null || !CausesValidation)
  130. return false;
  131. CultureInfo inv = CultureInfo.InvariantCulture;
  132. string input_type = Type;
  133. if (0 == String.Compare (input_type, "submit", true, inv) &&
  134. Page.Validators.Count > 0)
  135. return true;
  136. if (0 == String.Compare (input_type, "button", true, inv) &&
  137. Events [ServerClickEvent] != null)
  138. return true;
  139. return false;
  140. }
  141. #endif
  142. protected override void RenderAttributes (HtmlTextWriter writer)
  143. {
  144. #if NET_2_0
  145. CultureInfo inv = CultureInfo.InvariantCulture;
  146. string input_type = Type;
  147. if (0 != String.Compare (input_type, "reset", true, inv) &&
  148. ((0 == String.Compare (input_type, "submit", true, inv)) ||
  149. (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
  150. string onclick = String.Empty;
  151. if (Attributes ["onclick"] != null) {
  152. onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
  153. Attributes.Remove ("onclick");
  154. }
  155. if (Page != null) {
  156. PostBackOptions options = GetPostBackOptions ();
  157. Page.ClientScript.RegisterForEventValidation (options);
  158. onclick += Page.ClientScript.GetPostBackEventReference (options);
  159. }
  160. if (onclick.Length > 0) {
  161. writer.WriteAttribute ("onclick", onclick);
  162. writer.WriteAttribute ("language", "javascript");
  163. }
  164. }
  165. #else
  166. if (RenderOnClick ()) {
  167. string oc = null;
  168. ClientScriptManager csm = new ClientScriptManager (Page);
  169. if (Page.AreValidatorsUplevel ()) {
  170. oc = csm.GetClientValidationEvent ();
  171. } else if (Events [ServerClickEvent] != null) {
  172. oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
  173. }
  174. if (oc != null) {
  175. writer.WriteAttribute ("language", "javascript");
  176. writer.WriteAttribute ("onclick", oc);
  177. }
  178. }
  179. #endif
  180. Attributes.Remove ("CausesValidation");
  181. #if NET_2_0
  182. // LAMESPEC: MS doesn't actually remove this
  183. //attribute. it shows up in the rendered
  184. //output.
  185. // Attributes.Remove("ValidationGroup");
  186. #endif
  187. base.RenderAttributes (writer);
  188. }
  189. #if NET_2_0
  190. PostBackOptions GetPostBackOptions () {
  191. PostBackOptions options = new PostBackOptions (this);
  192. options.ValidationGroup = null;
  193. options.ActionUrl = null;
  194. options.Argument = "";
  195. options.RequiresJavaScriptProtocol = false;
  196. options.ClientSubmit = (0 != String.Compare (Type, "submit", true, CultureInfo.InvariantCulture));
  197. options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
  198. if (options.PerformValidation)
  199. options.ValidationGroup = ValidationGroup;
  200. return options;
  201. }
  202. #endif
  203. [WebSysDescription("")]
  204. [WebCategory("Action")]
  205. public event EventHandler ServerClick {
  206. add { Events.AddHandler (ServerClickEvent, value); }
  207. remove { Events.RemoveHandler (ServerClickEvent, value); }
  208. }
  209. }
  210. }