LinkButton.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // System.Web.UI.WebControls.LinkButton.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [DefaultEvent("Click")]
  39. [DefaultProperty("Text")]
  40. [ControlBuilder(typeof(LinkButtonControlBuilder))]
  41. [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  42. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  43. [ParseChildren(false)]
  44. [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
  45. public class LinkButton : WebControl, IPostBackEventHandler
  46. {
  47. private static readonly object ClickEvent = new object();
  48. private static readonly object CommandEvent = new object();
  49. public LinkButton () : base (HtmlTextWriterTag.A)
  50. {
  51. }
  52. [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
  53. [WebSysDescription ("Determines if validation is performed when clicked.")]
  54. public bool CausesValidation
  55. {
  56. get {
  57. object o = ViewState ["CausesValidation"];
  58. return (o == null) ? true : (bool) o;
  59. }
  60. set { ViewState ["CausesValidation"] = value; }
  61. }
  62. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  63. [WebSysDescription ("An argument for the Command of this control.")]
  64. public string CommandArgument
  65. {
  66. get {
  67. object o = ViewState ["CommandArgument"];
  68. return (o == null) ? String.Empty : (string) o;
  69. }
  70. set { ViewState ["CommandArgument"] = value; }
  71. }
  72. [DefaultValue (""), WebCategory ("Behavior")]
  73. [WebSysDescription ("The name of the Command of this control.")]
  74. public string CommandName
  75. {
  76. get {
  77. object o = ViewState ["CommandName"];
  78. return (o == null) ? String.Empty : (string) o;
  79. }
  80. set { ViewState ["CommandName"] = value; }
  81. }
  82. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  83. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  84. [WebSysDescription ("The text that should be shown on this LinkButton.")]
  85. public virtual string Text
  86. {
  87. get {
  88. object o = ViewState ["Text"];
  89. return (o == null) ? String.Empty : (string) o;
  90. }
  91. set { ViewState ["Text"] = value; }
  92. }
  93. [WebCategory ("Action")]
  94. [WebSysDescription ("Raised when the LinkButton is clicked.")]
  95. public event EventHandler Click
  96. {
  97. add { Events.AddHandler(ClickEvent, value); }
  98. remove { Events.RemoveHandler(ClickEvent, value); }
  99. }
  100. [WebCategory ("Action")]
  101. [WebSysDescription ("Raised when a LinkButton Command is executed.")]
  102. public event CommandEventHandler Command
  103. {
  104. add { Events.AddHandler(CommandEvent, value); }
  105. remove { Events.RemoveHandler(CommandEvent, value); }
  106. }
  107. protected virtual void OnClick (EventArgs e)
  108. {
  109. if(Events != null){
  110. EventHandler eh = (EventHandler) (Events [ClickEvent]);
  111. if (eh != null)
  112. eh (this, e);
  113. }
  114. }
  115. protected virtual void OnCommand (CommandEventArgs e)
  116. {
  117. if(Events != null){
  118. CommandEventHandler ceh = (CommandEventHandler) (Events [CommandEvent]);
  119. if (ceh != null)
  120. ceh (this, e);
  121. }
  122. RaiseBubbleEvent (this, e);
  123. }
  124. protected override void OnPreRender (EventArgs e)
  125. {
  126. base.OnPreRender(e);
  127. }
  128. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  129. {
  130. if (CausesValidation)
  131. Page.Validate ();
  132. OnClick (EventArgs.Empty);
  133. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  134. }
  135. protected override void AddAttributesToRender (HtmlTextWriter writer)
  136. {
  137. base.AddAttributesToRender (writer);
  138. if (Enabled && Page != null){
  139. if (CausesValidation && Page.Validators.Count > 0){
  140. writer.AddAttribute (HtmlTextWriterAttribute.Href,
  141. "javascript:" +
  142. Utils.GetClientValidatedPostBack (this));
  143. return;
  144. }
  145. writer.AddAttribute (HtmlTextWriterAttribute.Href,
  146. Page.GetPostBackClientHyperlink (this, ""));
  147. }
  148. }
  149. protected override void AddParsedSubObject (object obj)
  150. {
  151. if (HasControls ()){
  152. base.AddParsedSubObject (obj);
  153. return;
  154. }
  155. if (obj is LiteralControl){
  156. Text = ((LiteralControl) obj).Text;
  157. return;
  158. }
  159. if (Text.Length > 0){
  160. base.AddParsedSubObject (new LiteralControl (Text));
  161. Text = String.Empty;
  162. }
  163. base.AddParsedSubObject (obj);
  164. }
  165. protected override void LoadViewState (object savedState)
  166. {
  167. if (savedState != null){
  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. base.RenderContents (writer);
  178. return;
  179. }
  180. writer.Write (Text);
  181. }
  182. }
  183. }