LinkButton.cs 4.0 KB

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