HtmlButton.cs 3.8 KB

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