HtmlInputButton.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Security.Permissions;
  30. namespace System.Web.UI.HtmlControls {
  31. // CAS
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [DefaultEventAttribute ("ServerClick")]
  36. #if NET_2_0
  37. [SupportsEventValidation]
  38. #endif
  39. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
  40. private static readonly object ServerClickEvent = new object();
  41. public HtmlInputButton () : this ("button")
  42. {
  43. }
  44. public HtmlInputButton (string type) : base (type)
  45. {
  46. }
  47. [DefaultValue(true)]
  48. [WebSysDescription("")]
  49. [WebCategory("Behavior")]
  50. #if NET_2_0
  51. public virtual
  52. #else
  53. public
  54. #endif
  55. bool CausesValidation {
  56. get {
  57. string flag = Attributes["CausesValidation"];
  58. if (flag == null)
  59. return true;
  60. return Boolean.Parse (flag);
  61. }
  62. set {
  63. Attributes ["CausesValidation"] = value.ToString();
  64. }
  65. }
  66. #if NET_2_0
  67. [DefaultValue ("")]
  68. public string ValidationGroup
  69. {
  70. get {
  71. string group = Attributes["ValidationGroup"];
  72. if (group == null)
  73. return "";
  74. return group;
  75. }
  76. set {
  77. if (value == null)
  78. Attributes.Remove ("ValidationGroup");
  79. else
  80. Attributes["ValidationGroup"] = value;
  81. }
  82. }
  83. #endif
  84. void RaisePostBackEventInternal (string eventArgument)
  85. {
  86. if (CausesValidation)
  87. #if NET_2_0
  88. Page.Validate (ValidationGroup);
  89. #else
  90. Page.Validate ();
  91. #endif
  92. OnServerClick (EventArgs.Empty);
  93. }
  94. #if NET_2_0
  95. protected virtual void RaisePostBackEvent (string eventArgument)
  96. {
  97. RaisePostBackEventInternal (eventArgument);
  98. }
  99. #endif
  100. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  101. {
  102. #if NET_2_0
  103. RaisePostBackEvent (eventArgument);
  104. #else
  105. RaisePostBackEventInternal (eventArgument);
  106. #endif
  107. }
  108. #if NET_2_0
  109. protected internal
  110. #else
  111. protected
  112. #endif
  113. override void OnPreRender (EventArgs e)
  114. {
  115. base.OnPreRender (e);
  116. if (Events [ServerClickEvent] != null)
  117. Page.RequiresPostBackScript ();
  118. }
  119. protected virtual void OnServerClick (EventArgs e)
  120. {
  121. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  122. if (server_click != null)
  123. server_click (this, e);
  124. }
  125. protected override void RenderAttributes (HtmlTextWriter writer)
  126. {
  127. if (CausesValidation && Page != null) {
  128. string oc = null;
  129. ClientScriptManager csm = new ClientScriptManager (Page);
  130. if (Page.AreValidatorsUplevel ()) {
  131. oc = csm.GetClientValidationEvent ();
  132. } else if (Events [ServerClickEvent] != null) {
  133. oc = Attributes ["onclick"] + " " + csm.GetPostBackClientEvent (this, "");
  134. }
  135. if (oc != null) {
  136. writer.WriteAttribute ("language", "javascript");
  137. writer.WriteAttribute ("onclick", oc);
  138. }
  139. }
  140. Attributes.Remove ("CausesValidation");
  141. #if NET_2_0
  142. // LAMESPEC: MS doesn't actually remove this
  143. //attribute. it shows up in the rendered
  144. //output.
  145. // Attributes.Remove("ValidationGroup");
  146. #endif
  147. base.RenderAttributes (writer);
  148. }
  149. [WebSysDescription("")]
  150. [WebCategory("Action")]
  151. public event EventHandler ServerClick {
  152. add { Events.AddHandler (ServerClickEvent, value); }
  153. remove { Events.RemoveHandler (ServerClickEvent, value); }
  154. }
  155. }
  156. }