HtmlButton.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  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:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  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.
  20. //
  21. //
  22. // System.Web.UI.HtmlControls.HtmlButton
  23. //
  24. // Authors:
  25. // Jackson Harper ([email protected])
  26. //
  27. // (C) 2005 Novell, Inc.
  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. [DefaultEvent("ServerClick")]
  36. #if NET_2_0
  37. [SupportsEventValidation]
  38. #endif
  39. public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
  40. static readonly object ServerClickEvent = new object();
  41. public HtmlButton () : base ("button")
  42. {
  43. }
  44. [DefaultValue(true)]
  45. [WebSysDescription("")]
  46. [WebCategory("Behavior")]
  47. #if NET_2_0
  48. public virtual
  49. #else
  50. public
  51. #endif
  52. bool CausesValidation {
  53. get {
  54. return ViewState.GetBool ("CausesValidation", true);
  55. }
  56. set {
  57. ViewState ["CausesValidation"] = value;
  58. }
  59. }
  60. #if NET_2_0
  61. [DefaultValue ("")]
  62. public virtual string ValidationGroup
  63. {
  64. get {
  65. return ViewState.GetString ("ValidationGroup", "");
  66. }
  67. set {
  68. ViewState ["ValidationGroup"] = value;
  69. }
  70. }
  71. #endif
  72. #if NET_2_0
  73. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  74. {
  75. RaisePostBackEvent (eventArgument);
  76. }
  77. protected virtual void RaisePostBackEvent (string eventArgument)
  78. #else
  79. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  80. #endif
  81. {
  82. #if NET_2_0
  83. ValidateEvent (UniqueID, eventArgument);
  84. #endif
  85. if (CausesValidation)
  86. #if NET_2_0
  87. Page.Validate (ValidationGroup);
  88. #else
  89. Page.Validate ();
  90. #endif
  91. OnServerClick (EventArgs.Empty);
  92. }
  93. #if NET_2_0
  94. protected internal
  95. #else
  96. protected
  97. #endif
  98. override void OnPreRender (EventArgs e)
  99. {
  100. base.OnPreRender (e);
  101. }
  102. protected virtual void OnServerClick (EventArgs e)
  103. {
  104. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  105. if (server_click != null)
  106. server_click (this, e);
  107. }
  108. protected override void RenderAttributes (HtmlTextWriter writer)
  109. {
  110. #if NET_2_0
  111. if (Page != null && Events [ServerClickEvent] != null) {
  112. PostBackOptions options = GetPostBackOptions ();
  113. Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options, true);
  114. writer.WriteAttribute ("language", "javascript");
  115. }
  116. #else
  117. ClientScriptManager csm = new ClientScriptManager (Page);
  118. bool postback = false;
  119. if (Page != null && Events [ServerClickEvent] != null)
  120. postback = true;
  121. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  122. if (postback)
  123. writer.WriteAttribute ("onclick",
  124. String.Concat ("javascript:{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) ",
  125. csm.GetPostBackEventReference (this, String.Empty), "}"));
  126. else
  127. writer.WriteAttribute ("onclick",
  128. "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
  129. writer.WriteAttribute ("language", "javascript");
  130. }
  131. else if (postback) {
  132. writer.WriteAttribute ("onclick",
  133. Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  134. writer.WriteAttribute ("language", "javascript");
  135. }
  136. #endif
  137. base.RenderAttributes (writer);
  138. }
  139. #if NET_2_0
  140. PostBackOptions GetPostBackOptions () {
  141. PostBackOptions options = new PostBackOptions (this);
  142. options.ValidationGroup = null;
  143. options.ActionUrl = null;
  144. options.Argument = String.Empty;
  145. options.RequiresJavaScriptProtocol = false;
  146. options.ClientSubmit = true;
  147. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  148. if (options.PerformValidation)
  149. options.ValidationGroup = ValidationGroup;
  150. return options;
  151. }
  152. #endif
  153. [WebSysDescription("")]
  154. [WebCategory("Action")]
  155. public event EventHandler ServerClick {
  156. add { Events.AddHandler (ServerClickEvent, value); }
  157. remove { Events.RemoveHandler (ServerClickEvent, value); }
  158. }
  159. }
  160. }