2
0

HtmlButton.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
  37. private static readonly object ServerClickEvent = new object();
  38. public HtmlButton () : base ("button")
  39. {
  40. }
  41. [DefaultValue(true)]
  42. [WebSysDescription("")]
  43. [WebCategory("Behavior")]
  44. #if NET_2_0
  45. public virtual
  46. #else
  47. public
  48. #endif
  49. bool CausesValidation {
  50. get {
  51. return ViewState.GetBool ("CausesValidation", true);
  52. }
  53. set {
  54. ViewState ["CausesValidation"] = value;
  55. }
  56. }
  57. #if NET_2_0
  58. [DefaultValue ("")]
  59. public string ValidationGroup
  60. {
  61. get {
  62. return ViewState.GetString ("ValidationGroup", "");
  63. }
  64. set {
  65. ViewState ["ValidationGroup"] = value;
  66. }
  67. }
  68. #endif
  69. #if NET_2_0
  70. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  71. {
  72. RaisePostBackEvent (eventArgument);
  73. }
  74. protected virtual void RaisePostBackEvent (string eventArgument)
  75. #else
  76. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  77. #endif
  78. {
  79. if (CausesValidation)
  80. #if NET_2_0
  81. Page.Validate (ValidationGroup);
  82. #else
  83. Page.Validate ();
  84. #endif
  85. OnServerClick (EventArgs.Empty);
  86. }
  87. #if NET_2_0
  88. protected internal
  89. #else
  90. protected
  91. #endif
  92. override void OnPreRender (EventArgs e)
  93. {
  94. base.OnPreRender (e);
  95. }
  96. protected virtual void OnServerClick (EventArgs e)
  97. {
  98. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  99. if (server_click != null)
  100. server_click (this, e);
  101. }
  102. protected override void RenderAttributes (HtmlTextWriter writer)
  103. {
  104. ClientScriptManager csm = new ClientScriptManager (Page);
  105. bool postback = false;
  106. if (Page != null && Events [ServerClickEvent] != null)
  107. postback = true;
  108. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  109. if (postback)
  110. writer.WriteAttribute ("onclick",
  111. String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0}}}",
  112. csm.GetPostBackEventReference (this, String.Empty)));
  113. else
  114. writer.WriteAttribute ("onclick",
  115. "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
  116. writer.WriteAttribute ("language", "javascript");
  117. }
  118. else if (postback) {
  119. writer.AddAttribute ("onclick",
  120. Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  121. writer.WriteAttribute ("language", "javascript");
  122. }
  123. base.RenderAttributes (writer);
  124. }
  125. [WebSysDescription("")]
  126. [WebCategory("Action")]
  127. public event EventHandler ServerClick {
  128. add { Events.AddHandler (ServerClickEvent, value); }
  129. remove { Events.RemoveHandler (ServerClickEvent, value); }
  130. }
  131. }
  132. }