LinkButton.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // System.Web.UI.WebControls.LinkButton.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.Security.Permissions;
  30. namespace System.Web.UI.WebControls
  31. {
  32. // CAS
  33. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. // attributes
  36. [ControlBuilder(typeof(LinkButtonControlBuilder))]
  37. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  38. [DefaultEvent("Click")]
  39. [DefaultProperty("Text")]
  40. [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [ParseChildren(false)]
  42. [SupportsEventValidation]
  43. [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
  44. public class LinkButton : WebControl, IPostBackEventHandler, IButtonControl
  45. {
  46. public LinkButton () : base (HtmlTextWriterTag.A)
  47. {
  48. }
  49. protected override void AddAttributesToRender (HtmlTextWriter w)
  50. {
  51. Page page = Page;
  52. if (page != null)
  53. page.VerifyRenderingInServerForm (this);
  54. bool enabled = IsEnabled;
  55. string onclick = OnClientClick;
  56. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
  57. if (HasAttributes && Attributes ["onclick"] != null) {
  58. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
  59. Attributes.Remove ("onclick");
  60. }
  61. if (onclick.Length > 0)
  62. w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
  63. if (enabled && page != null) {
  64. PostBackOptions options = GetPostBackOptions ();
  65. string href = page.ClientScript.GetPostBackEventReference (options, true);
  66. w.AddAttribute (HtmlTextWriterAttribute.Href, href);
  67. }
  68. base.AddAttributesToRender (w);
  69. AddDisplayStyleAttribute (w);
  70. }
  71. protected virtual void RaisePostBackEvent (string eventArgument)
  72. {
  73. ValidateEvent (UniqueID, eventArgument);
  74. if (CausesValidation) {
  75. Page page = Page;
  76. if (page != null)
  77. page.Validate (ValidationGroup);
  78. }
  79. OnClick (EventArgs.Empty);
  80. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  81. }
  82. void IPostBackEventHandler.RaisePostBackEvent (string ea)
  83. {
  84. RaisePostBackEvent (ea);
  85. }
  86. protected override void AddParsedSubObject (object obj)
  87. {
  88. if (HasControls ()) {
  89. base.AddParsedSubObject (obj);
  90. return;
  91. }
  92. LiteralControl lc = obj as LiteralControl;
  93. if (lc == null) {
  94. string s = Text;
  95. if (s.Length != 0) {
  96. Text = null;
  97. Controls.Add (new LiteralControl (s));
  98. }
  99. base.AddParsedSubObject (obj);
  100. } else
  101. Text = lc.Text;
  102. }
  103. protected virtual PostBackOptions GetPostBackOptions ()
  104. {
  105. PostBackOptions options = new PostBackOptions (this);
  106. Page page = Page;
  107. options.ActionUrl = (PostBackUrl.Length > 0 ?
  108. #if TARGET_J2EE
  109. CreateActionUrl (PostBackUrl)
  110. #else
  111. page != null ? page.ResolveClientUrl (PostBackUrl) : PostBackUrl
  112. #endif
  113. : null);
  114. options.ValidationGroup = null;
  115. options.Argument = String.Empty;
  116. options.ClientSubmit = true;
  117. options.RequiresJavaScriptProtocol = true;
  118. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  119. if (options.PerformValidation)
  120. options.ValidationGroup = ValidationGroup;
  121. return options;
  122. }
  123. protected override void LoadViewState (object savedState)
  124. {
  125. base.LoadViewState (savedState);
  126. // Make sure we clear child controls when this happens
  127. if (ViewState ["Text"] != null)
  128. Text = (string) ViewState ["Text"];
  129. }
  130. [MonoTODO ("Why override?")]
  131. protected internal override void OnPreRender (EventArgs e)
  132. {
  133. base.OnPreRender (e);
  134. }
  135. protected internal override void RenderContents (HtmlTextWriter writer)
  136. {
  137. if (HasControls () || HasRenderMethodDelegate ())
  138. base.RenderContents (writer);
  139. else
  140. writer.Write (Text);
  141. }
  142. [DefaultValue(true)]
  143. [WebSysDescription ("")]
  144. [WebCategory ("Behavior")]
  145. [Themeable (false)]
  146. public virtual bool CausesValidation {
  147. get { return ViewState.GetBool ("CausesValidation", true); }
  148. set { ViewState ["CausesValidation"] = value; }
  149. }
  150. [Bindable(true)]
  151. [DefaultValue("")]
  152. [WebSysDescription ("")]
  153. [WebCategory ("Behavior")]
  154. [Themeable (false)]
  155. public string CommandArgument {
  156. get { return ViewState.GetString ("CommandArgument", String.Empty); }
  157. set { ViewState ["CommandArgument"] = value; }
  158. }
  159. [DefaultValue("")]
  160. [WebSysDescription ("")]
  161. [WebCategory ("Behavior")]
  162. [Themeable (false)]
  163. public string CommandName {
  164. get { return ViewState.GetString ("CommandName", String.Empty); }
  165. set { ViewState ["CommandName"] = value; }
  166. }
  167. [DefaultValue ("")]
  168. [Themeable (false)]
  169. [WebSysDescription ("")]
  170. [WebCategoryAttribute ("Behavior")]
  171. public virtual string OnClientClick
  172. {
  173. get { return ViewState.GetString ("OnClientClick", String.Empty); }
  174. set { ViewState ["OnClientClick"] = value; }
  175. }
  176. [Bindable(true)]
  177. [DefaultValue("")]
  178. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  179. [Localizable (true)]
  180. [WebSysDescription ("")]
  181. [WebCategory ("Appearance")]
  182. public virtual string Text {
  183. get { return ViewState.GetString ("Text", String.Empty); }
  184. set {
  185. ViewState ["Text"] = value;
  186. if (HasControls ())
  187. Controls.Clear ();
  188. }
  189. }
  190. protected virtual void OnClick (EventArgs e)
  191. {
  192. EventHandler h = (EventHandler) Events [ClickEvent];
  193. if (h != null)
  194. h (this, e);
  195. }
  196. static readonly object ClickEvent = new object ();
  197. [WebSysDescription ("")]
  198. [WebCategory ("Action")]
  199. public event EventHandler Click {
  200. add { Events.AddHandler (ClickEvent, value); }
  201. remove { Events.RemoveHandler (ClickEvent, value); }
  202. }
  203. protected virtual void OnCommand (CommandEventArgs e)
  204. {
  205. CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
  206. if (h != null)
  207. h (this, e);
  208. RaiseBubbleEvent (this, e);
  209. }
  210. static readonly object CommandEvent = new object ();
  211. [WebSysDescription ("")]
  212. [WebCategory ("Action")]
  213. public event CommandEventHandler Command {
  214. add { Events.AddHandler (CommandEvent, value); }
  215. remove { Events.RemoveHandler (CommandEvent, value); }
  216. }
  217. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  218. [Themeable (false)]
  219. [UrlProperty ("*.aspx")]
  220. [DefaultValue ("")]
  221. public virtual string PostBackUrl {
  222. get { return ViewState.GetString ("PostBackUrl", String.Empty); }
  223. set { ViewState["PostBackUrl"] = value; }
  224. }
  225. [DefaultValue ("")]
  226. [Themeable (false)]
  227. [WebSysDescription ("")]
  228. [WebCategoryAttribute ("Behavior")]
  229. public virtual string ValidationGroup {
  230. get { return ViewState.GetString ("ValidationGroup", String.Empty); }
  231. set { ViewState ["ValidationGroup"] = value; }
  232. }
  233. #if NET_4_0
  234. public override bool SupportsDisabledAttribute {
  235. get { return RenderingCompatibilityLessThan40; }
  236. }
  237. #endif
  238. }
  239. }