Button.cs 3.6 KB

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