HtmlInputButton.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. bool RenderOnClick ()
  127. {
  128. if (Page == null || !CausesValidation)
  129. return false;
  130. CultureInfo inv = CultureInfo.InvariantCulture;
  131. string input_type = Type;
  132. if (0 == String.Compare (input_type, "submit", true, inv) &&
  133. Page.Validators.Count > 0)
  134. return true;
  135. if (0 == String.Compare (input_type, "button", true, inv) &&
  136. Events [ServerClickEvent] != null)
  137. return true;
  138. return false;
  139. }
  140. protected override void RenderAttributes (HtmlTextWriter writer)
  141. {
  142. if (RenderOnClick ()) {
  143. string oc = null;
  144. ClientScriptManager csm = new ClientScriptManager (Page);
  145. if (Page.AreValidatorsUplevel ()) {
  146. oc = csm.GetClientValidationEvent ();
  147. } else if (Events [ServerClickEvent] != null) {
  148. oc = Attributes ["onclick"] + " " + csm.GetPostBackClientEvent (this, "");
  149. }
  150. if (oc != null) {
  151. writer.WriteAttribute ("language", "javascript");
  152. writer.WriteAttribute ("onclick", oc);
  153. }
  154. }
  155. Attributes.Remove ("CausesValidation");
  156. #if NET_2_0
  157. // LAMESPEC: MS doesn't actually remove this
  158. //attribute. it shows up in the rendered
  159. //output.
  160. // Attributes.Remove("ValidationGroup");
  161. #endif
  162. base.RenderAttributes (writer);
  163. }
  164. [WebSysDescription("")]
  165. [WebCategory("Action")]
  166. public event EventHandler ServerClick {
  167. add { Events.AddHandler (ServerClickEvent, value); }
  168. remove { Events.RemoveHandler (ServerClickEvent, value); }
  169. }
  170. }
  171. }