Button.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: Button
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.ComponentModel;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class Button : WebControl, IPostBackEventHandler
  20. {
  21. private static readonly object ClickEvent = new object();
  22. private static readonly object CommandEvent = new object();
  23. //private EventHandlerList ehList;
  24. public Button(): base(HtmlTextWriterTag.Button)
  25. {
  26. }
  27. public bool CausesValidation
  28. {
  29. get
  30. {
  31. Object cv = ViewState["CausesValidation"];
  32. if(cv!=null)
  33. return (Boolean)cv;
  34. return true;
  35. }
  36. set
  37. {
  38. ViewState["CausesValidation"] = value;
  39. }
  40. }
  41. public string CommandArgument
  42. {
  43. get
  44. {
  45. string ca = (string) ViewState["CommandArgument"];
  46. if(ca!=null)
  47. return ca;
  48. return String.Empty;
  49. }
  50. set
  51. {
  52. ViewState["CommandArgument"] = value;
  53. }
  54. }
  55. public string CommandName
  56. {
  57. get
  58. {
  59. string cn = (string)ViewState["CommandName"];
  60. if(cn!=null)
  61. return cn;
  62. return String.Empty;
  63. }
  64. set
  65. {
  66. ViewState["CommandName"] = value;
  67. }
  68. }
  69. public string Text
  70. {
  71. get
  72. {
  73. string text = (string)ViewState["Text"];
  74. if(text!=null)
  75. return text;
  76. return String.Empty;
  77. }
  78. set
  79. {
  80. ViewState["Text"] = value;
  81. }
  82. }
  83. public event EventHandler Click
  84. {
  85. add
  86. {
  87. Events.AddHandler(ClickEvent, value);
  88. }
  89. remove
  90. {
  91. Events.RemoveHandler(ClickEvent, value);
  92. }
  93. }
  94. public event CommandEventHandler Command
  95. {
  96. add
  97. {
  98. Events.AddHandler(CommandEvent, value);
  99. }
  100. remove
  101. {
  102. Events.RemoveHandler(CommandEvent, value);
  103. }
  104. }
  105. protected virtual void OnClick(EventArgs e)
  106. {
  107. if(Events != null)
  108. {
  109. EventHandler eh = (EventHandler)(Events[ClickEvent]);
  110. if(eh!= null)
  111. eh(this,e);
  112. }
  113. }
  114. protected virtual void OnCommand(CommandEventArgs e)
  115. {
  116. if(Events != null)
  117. {
  118. EventHandler eh = (EventHandler)(Events[CommandEvent]);
  119. if(eh!= null)
  120. eh(this,e);
  121. }
  122. }
  123. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  124. {
  125. if(CausesValidation)
  126. {
  127. Page.Validate();
  128. OnClick(new EventArgs());
  129. OnCommand(new CommandEventArgs(CommandName, CommandArgument));
  130. }
  131. }
  132. protected override void AddAttributesToRender(HtmlTextWriter writer)
  133. {
  134. writer.AddAttribute(HtmlTextWriterAttribute.Type,"submit");
  135. writer.AddAttribute(HtmlTextWriterAttribute.Name,base.UniqueID);
  136. writer.AddAttribute(HtmlTextWriterAttribute.Value,Text);
  137. if(Page!=null && CausesValidation && Page.Validators.Count > 0)
  138. {
  139. writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent());
  140. writer.AddAttribute("language", "javascript");
  141. }
  142. AddAttributesToRender(writer);
  143. }
  144. protected override void RenderContents(HtmlTextWriter writer)
  145. {
  146. // Preventing subclasses to do anything
  147. }
  148. }
  149. }