LinkButton.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // System.Web.UI.WebControls.LinkButton.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005 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. // CAS
  32. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [ControlBuilder(typeof(LinkButtonControlBuilder))]
  36. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  37. [DefaultEvent("Click")]
  38. [DefaultProperty("Text")]
  39. [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. [ParseChildren(false)]
  41. #if NET_2_0
  42. [SupportsEventValidation]
  43. [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
  44. #else
  45. [ToolboxData("<{0}:LinkButton runat=server>LinkButton</{0}:LinkButton>")]
  46. #endif
  47. public class LinkButton : WebControl, IPostBackEventHandler
  48. #if NET_2_0
  49. , IButtonControl
  50. #endif
  51. {
  52. public LinkButton () : base (HtmlTextWriterTag.A)
  53. {
  54. }
  55. protected override void AddAttributesToRender (HtmlTextWriter w)
  56. {
  57. if (Page != null)
  58. Page.VerifyRenderingInServerForm (this);
  59. base.AddAttributesToRender (w);
  60. if (Page == null || !Enabled)
  61. return;
  62. if (CausesValidation && Page.AreValidatorsUplevel ()) {
  63. ClientScriptManager csm = new ClientScriptManager (Page);
  64. w.AddAttribute (HtmlTextWriterAttribute.Href,
  65. String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0};}}",
  66. csm.GetPostBackEventReference (this, String.Empty)));
  67. w.AddAttribute ("language", "javascript");
  68. } else {
  69. w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  70. }
  71. }
  72. #if NET_2_0
  73. [MonoTODO]
  74. protected virtual void RaisePostBackEvent (string eventArgument)
  75. {
  76. throw new NotImplementedException ();
  77. }
  78. #endif
  79. void IPostBackEventHandler.RaisePostBackEvent (string ea)
  80. {
  81. if (CausesValidation)
  82. #if NET_2_0
  83. Page.Validate (ValidationGroup);
  84. #else
  85. Page.Validate ();
  86. #endif
  87. OnClick (EventArgs.Empty);
  88. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  89. }
  90. protected override void AddParsedSubObject (object obj)
  91. {
  92. if (HasControls ()) {
  93. base.AddParsedSubObject (obj);
  94. return;
  95. }
  96. LiteralControl lc = obj as LiteralControl;
  97. if (lc == null) {
  98. string s = Text;
  99. if (s.Length != 0) {
  100. Text = null;
  101. Controls.Add (new LiteralControl (s));
  102. }
  103. base.AddParsedSubObject (obj);
  104. } else {
  105. Text = lc.Text;
  106. }
  107. }
  108. #if NET_2_0
  109. [MonoTODO]
  110. protected virtual PostBackOptions GetPostBackOptions ()
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. #endif
  115. protected override void LoadViewState (object savedState)
  116. {
  117. base.LoadViewState (savedState);
  118. // Make sure we clear child controls when this happens
  119. if (ViewState ["Text"] != null)
  120. Text = (string) ViewState ["Text"];
  121. }
  122. [MonoTODO ("Why override?")]
  123. #if NET_2_0
  124. protected internal
  125. #else
  126. protected
  127. #endif
  128. override void OnPreRender (EventArgs e)
  129. {
  130. base.OnPreRender (e);
  131. }
  132. #if NET_2_0
  133. protected internal
  134. #else
  135. protected
  136. #endif
  137. override void RenderContents (HtmlTextWriter writer)
  138. {
  139. if (HasControls ())
  140. base.RenderContents (writer);
  141. else
  142. writer.Write (Text);
  143. }
  144. #if ONLY_1_1
  145. [Bindable(false)]
  146. #endif
  147. [DefaultValue(true)]
  148. [WebSysDescription ("")]
  149. [WebCategory ("Behavior")]
  150. #if NET_2_0
  151. [Themeable (false)]
  152. public virtual
  153. #else
  154. public
  155. #endif
  156. bool CausesValidation {
  157. get {
  158. return ViewState.GetBool ("CausesValidation", true);
  159. }
  160. set {
  161. ViewState ["CausesValidation"] = value;
  162. }
  163. }
  164. [Bindable(true)]
  165. [DefaultValue("")]
  166. [WebSysDescription ("")]
  167. [WebCategory ("Behavior")]
  168. #if NET_2_0
  169. [Themeable (false)]
  170. #endif
  171. public string CommandArgument {
  172. get {
  173. return ViewState.GetString ("CommandArgument", "");
  174. }
  175. set {
  176. ViewState ["CommandArgument"] = value;
  177. }
  178. }
  179. [DefaultValue("")]
  180. [WebSysDescription ("")]
  181. [WebCategory ("Behavior")]
  182. #if NET_2_0
  183. [Themeable (false)]
  184. #endif
  185. public string CommandName {
  186. get {
  187. return ViewState.GetString ("CommandName", "");
  188. }
  189. set {
  190. ViewState ["CommandName"] = value;
  191. }
  192. }
  193. #if NET_2_0
  194. [DefaultValue ("")]
  195. [Themeable (false)]
  196. [MonoTODO]
  197. [WebSysDescription ("")]
  198. [WebCategoryAttribute ("Behavior")]
  199. public virtual string OnClientClick
  200. {
  201. get {
  202. throw new NotImplementedException ();
  203. }
  204. set {
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. #endif
  209. [Bindable(true)]
  210. [DefaultValue("")]
  211. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  212. #if NET_2_0
  213. [Localizable (true)]
  214. #endif
  215. [WebSysDescription ("")]
  216. [WebCategory ("Appearance")]
  217. public virtual string Text {
  218. get {
  219. return ViewState.GetString ("Text", "");
  220. }
  221. set {
  222. ViewState ["Text"] = value;
  223. if (HasControls ())
  224. Controls.Clear ();
  225. }
  226. }
  227. protected virtual void OnClick (EventArgs e)
  228. {
  229. EventHandler h = (EventHandler) Events [ClickEvent];
  230. if (h != null)
  231. h (this, e);
  232. }
  233. static readonly object ClickEvent = new object ();
  234. [WebSysDescription ("")]
  235. [WebCategory ("Action")]
  236. public event EventHandler Click {
  237. add { Events.AddHandler (ClickEvent, value); }
  238. remove { Events.RemoveHandler (ClickEvent, value); }
  239. }
  240. protected virtual void OnCommand (CommandEventArgs e)
  241. {
  242. CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
  243. if (h != null)
  244. h (this, e);
  245. RaiseBubbleEvent (this, e);
  246. }
  247. static readonly object CommandEvent = new object ();
  248. [WebSysDescription ("")]
  249. [WebCategory ("Action")]
  250. public event CommandEventHandler Command {
  251. add { Events.AddHandler (CommandEvent, value); }
  252. remove { Events.RemoveHandler (CommandEvent, value); }
  253. }
  254. #if NET_2_0
  255. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  256. [Themeable (false)]
  257. [UrlProperty ("*.aspx")]
  258. [DefaultValue ("")]
  259. [MonoTODO]
  260. public virtual string PostBackUrl {
  261. get {
  262. throw new NotImplementedException ();
  263. }
  264. set {
  265. throw new NotImplementedException ();
  266. }
  267. }
  268. [DefaultValue ("")]
  269. [Themeable (false)]
  270. [WebSysDescription ("")]
  271. [WebCategoryAttribute ("Behavior")]
  272. public virtual string ValidationGroup {
  273. get {
  274. return ViewState.GetString ("ValidationGroup", "");
  275. }
  276. set {
  277. ViewState ["ValidationGroup"] = value;
  278. }
  279. }
  280. #endif
  281. }
  282. }