LinkButton.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 virtual string Text
  68. {
  69. get
  70. {
  71. object o = ViewState["Text"];
  72. if(o != null)
  73. {
  74. return (string)o;
  75. }
  76. return String.Empty;
  77. }
  78. set
  79. {
  80. ViewState["Text"] = value;
  81. }
  82. }
  83. public event EventHandler Click
  84. {
  85. add
  86. {
  87. Events.AddHandler(ClickEvent, value);
  88. }
  89. remove
  90. {
  91. Events.RemoveHandler(ClickEvent, value);
  92. }
  93. }
  94. public event CommandEventHandler Command
  95. {
  96. add
  97. {
  98. Events.AddHandler(CommandEvent, value);
  99. }
  100. remove
  101. {
  102. Events.RemoveHandler(CommandEvent, value);
  103. }
  104. }
  105. protected virtual void OnClick(EventArgs e)
  106. {
  107. if(Events != null)
  108. {
  109. EventHandler eh = (EventHandler)(Events[ClickEvent]);
  110. if(eh != null)
  111. eh(this, e);
  112. }
  113. }
  114. protected virtual void OnCommand(CommandEventArgs e)
  115. {
  116. if(Events != null)
  117. {
  118. CommandEventHandler ceh = (CommandEventHandler)(Events[CommandEvent]);
  119. if(ceh != null)
  120. ceh(this, e);
  121. }
  122. }
  123. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  124. {
  125. if(CausesValidation)
  126. {
  127. Page.Validate();
  128. }
  129. OnClick(new EventArgs());
  130. OnCommand( new CommandEventArgs(CommandName, CommandArgument));
  131. }
  132. protected override void AddAttributesToRender(HtmlTextWriter writer)
  133. {
  134. base.AddAttributesToRender(writer);
  135. if(Enabled && Page != null)
  136. {
  137. if(CausesValidation && Page.Validators.Count > 0)
  138. {
  139. writer.AddAttribute(HtmlTextWriterAttribute.Href, "javscript:" + Utils.GetClientValidatedPostBack(this));
  140. return;
  141. }
  142. writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.GetPostBackClientHyperlink(this, ""));
  143. }
  144. }
  145. protected override void AddParsedSubObject(object obj)
  146. {
  147. if(HasControls())
  148. {
  149. AddParsedSubObject(obj);
  150. return;
  151. }
  152. if(obj is LiteralControl)
  153. {
  154. Text = ((LiteralControl)obj).Text;
  155. return;
  156. }
  157. if(Text.Length > 0)
  158. {
  159. AddParsedSubObject(Text);
  160. Text = String.Empty;
  161. }
  162. AddParsedSubObject(obj);
  163. }
  164. protected override void LoadViewState(object savedState)
  165. {
  166. if(savedState != null)
  167. {
  168. base.LoadViewState(savedState);
  169. string savedText = (string)ViewState["Text"];
  170. if(savedText != null)
  171. Text = savedText;
  172. }
  173. }
  174. protected override void RenderContents(HtmlTextWriter writer)
  175. {
  176. if(HasControls())
  177. {
  178. RenderContents(writer);
  179. return;
  180. }
  181. writer.Write(Text);
  182. }
  183. }
  184. }