LinkButton.cs 4.0 KB

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