Button.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. [DefaultEvent("Click")]
  20. [DefaultProperty("Text")]
  21. //TODO: [Designer("??")]
  22. //TODO: [DataBindingHandler("??UI.Design.TextDataBindingHandler??")]
  23. [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
  24. public class Button : WebControl, IPostBackEventHandler
  25. {
  26. private static readonly object ClickEvent = new object();
  27. private static readonly object CommandEvent = new object();
  28. //private EventHandlerList ehList;
  29. public Button(): base(HtmlTextWriterTag.Button)
  30. {
  31. }
  32. public bool CausesValidation
  33. {
  34. get
  35. {
  36. Object cv = ViewState["CausesValidation"];
  37. if(cv!=null)
  38. return (Boolean)cv;
  39. return true;
  40. }
  41. set
  42. {
  43. ViewState["CausesValidation"] = value;
  44. }
  45. }
  46. public string CommandArgument
  47. {
  48. get
  49. {
  50. string ca = (string) ViewState["CommandArgument"];
  51. if(ca!=null)
  52. return ca;
  53. return String.Empty;
  54. }
  55. set
  56. {
  57. ViewState["CommandArgument"] = value;
  58. }
  59. }
  60. public string CommandName
  61. {
  62. get
  63. {
  64. string cn = (string)ViewState["CommandName"];
  65. if(cn!=null)
  66. return cn;
  67. return String.Empty;
  68. }
  69. set
  70. {
  71. ViewState["CommandName"] = value;
  72. }
  73. }
  74. public string Text
  75. {
  76. get
  77. {
  78. string text = (string)ViewState["Text"];
  79. if(text!=null)
  80. return text;
  81. return String.Empty;
  82. }
  83. set
  84. {
  85. ViewState["Text"] = value;
  86. }
  87. }
  88. public event EventHandler Click
  89. {
  90. add
  91. {
  92. Events.AddHandler(ClickEvent, value);
  93. }
  94. remove
  95. {
  96. Events.RemoveHandler(ClickEvent, value);
  97. }
  98. }
  99. public event CommandEventHandler Command
  100. {
  101. add
  102. {
  103. Events.AddHandler(CommandEvent, value);
  104. }
  105. remove
  106. {
  107. Events.RemoveHandler(CommandEvent, value);
  108. }
  109. }
  110. protected virtual void OnClick(EventArgs e)
  111. {
  112. if(Events != null)
  113. {
  114. EventHandler eh = (EventHandler)(Events[ClickEvent]);
  115. if(eh!= null)
  116. eh(this,e);
  117. }
  118. }
  119. protected virtual void OnCommand(CommandEventArgs e)
  120. {
  121. if(Events != null)
  122. {
  123. EventHandler eh = (EventHandler)(Events[CommandEvent]);
  124. if(eh!= null)
  125. eh(this,e);
  126. }
  127. }
  128. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  129. {
  130. if(CausesValidation)
  131. {
  132. Page.Validate();
  133. OnClick(new EventArgs());
  134. OnCommand(new CommandEventArgs(CommandName, CommandArgument));
  135. }
  136. }
  137. protected override void AddAttributesToRender(HtmlTextWriter writer)
  138. {
  139. writer.AddAttribute(HtmlTextWriterAttribute.Type,"submit");
  140. writer.AddAttribute(HtmlTextWriterAttribute.Name,base.UniqueID);
  141. writer.AddAttribute(HtmlTextWriterAttribute.Value,Text);
  142. if(Page!=null && CausesValidation && Page.Validators.Count > 0)
  143. {
  144. writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onclick, Utils.GetClientValidatedEvent(Page));
  145. writer.AddAttribute("language", "javascript");
  146. }
  147. AddAttributesToRender(writer);
  148. }
  149. protected override void RenderContents(HtmlTextWriter writer)
  150. {
  151. // Preventing subclasses to do anything
  152. }
  153. }
  154. }