LinkButton.cs 3.5 KB

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