LinkButton.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: LinkButton
  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.Web;
  15. using System.Web.UI;
  16. using System.ComponentModel;
  17. namespace System.Web.UI.WebControls
  18. {
  19. [DefaultEvent("Click")]
  20. [DefaultProperty("Text")]
  21. //[Designer("??")]
  22. [ControlBuilder(typeof(LinkButtonControlBuilder))]
  23. //[DataBindingHandler("??")]
  24. [ParseChildren(false)]
  25. [PersistChildren(false)]
  26. [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
  27. public class LinkButton : WebControl, IPostBackEventHandler
  28. {
  29. private static readonly object ClickEvent = new object();
  30. private static readonly object CommandEvent = new object();
  31. public LinkButton () : base (HtmlTextWriterTag.A)
  32. {
  33. }
  34. public bool CausesValidation
  35. {
  36. get {
  37. object o = ViewState ["CausesValidation"];
  38. return (o == null) ? true : (bool) o;
  39. }
  40. set { ViewState ["CausesValidation"] = value; }
  41. }
  42. public string CommandArgument
  43. {
  44. get {
  45. object o = ViewState ["CommandArgument"];
  46. return (o == null) ? String.Empty : (string) o;
  47. }
  48. set { ViewState ["CommandArgument"] = value; }
  49. }
  50. public string CommandName
  51. {
  52. get {
  53. object o = ViewState ["CommandName"];
  54. return (o == null) ? String.Empty : (string) o;
  55. }
  56. set { ViewState ["CommandName"] = value; }
  57. }
  58. public virtual string Text
  59. {
  60. get {
  61. object o = ViewState ["Text"];
  62. return (o == null) ? String.Empty : (string) o;
  63. }
  64. set { ViewState ["Text"] = value; }
  65. }
  66. public event EventHandler Click
  67. {
  68. add { Events.AddHandler(ClickEvent, value); }
  69. remove { Events.RemoveHandler(ClickEvent, value); }
  70. }
  71. public event CommandEventHandler Command
  72. {
  73. add { Events.AddHandler(CommandEvent, value); }
  74. remove { Events.RemoveHandler(CommandEvent, value); }
  75. }
  76. protected virtual void OnClick (EventArgs e)
  77. {
  78. if(Events != null){
  79. EventHandler eh = (EventHandler) (Events [ClickEvent]);
  80. if (eh != null)
  81. eh (this, e);
  82. }
  83. }
  84. protected virtual void OnCommand (CommandEventArgs e)
  85. {
  86. if(Events != null){
  87. CommandEventHandler ceh = (CommandEventHandler) (Events [CommandEvent]);
  88. if (ceh != null)
  89. ceh (this, e);
  90. }
  91. }
  92. protected override void OnPreRender (EventArgs e)
  93. {
  94. base.OnPreRender(e);
  95. }
  96. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  97. {
  98. if (CausesValidation)
  99. Page.Validate ();
  100. OnClick (EventArgs.Empty);
  101. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  102. }
  103. protected override void AddAttributesToRender (HtmlTextWriter writer)
  104. {
  105. base.AddAttributesToRender (writer);
  106. if (Enabled && Page != null){
  107. if (CausesValidation && Page.Validators.Count > 0){
  108. writer.AddAttribute (HtmlTextWriterAttribute.Href,
  109. "javascript:" +
  110. Utils.GetClientValidatedPostBack (this));
  111. return;
  112. }
  113. writer.AddAttribute (HtmlTextWriterAttribute.Href,
  114. Page.GetPostBackClientHyperlink (this, ""));
  115. }
  116. }
  117. protected override void AddParsedSubObject (object obj)
  118. {
  119. if (HasControls ()){
  120. base.AddParsedSubObject (obj);
  121. return;
  122. }
  123. if (obj is LiteralControl){
  124. Text = ((LiteralControl) obj).Text;
  125. return;
  126. }
  127. if (Text.Length > 0){
  128. base.AddParsedSubObject (new LiteralControl (Text));
  129. Text = String.Empty;
  130. }
  131. base.AddParsedSubObject (obj);
  132. }
  133. protected override void LoadViewState (object savedState)
  134. {
  135. if (savedState != null){
  136. base.LoadViewState (savedState);
  137. string savedText = (string) ViewState ["Text"];
  138. if (savedText != null)
  139. Text = savedText;
  140. }
  141. }
  142. protected override void RenderContents (HtmlTextWriter writer)
  143. {
  144. if (HasControls ()){
  145. base.RenderContents (writer);
  146. return;
  147. }
  148. writer.Write (Text);
  149. }
  150. }
  151. }