LinkButton.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. namespace System.Web.UI.WebControls {
  30. [ControlBuilder(typeof(LinkButtonControlBuilder))]
  31. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  32. [DefaultEvent("Click")]
  33. [DefaultProperty("Text")]
  34. [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  35. [ParseChildren(false)]
  36. #if NET_2_0
  37. [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
  38. #else
  39. [ToolboxData("<{0}:LinkButton runat=server>LinkButton</{0}:LinkButton>")]
  40. #endif
  41. public class LinkButton : WebControl, IPostBackEventHandler
  42. #if NET_2_0
  43. , IButtonControl
  44. #endif
  45. {
  46. public LinkButton () : base (HtmlTextWriterTag.A)
  47. {
  48. }
  49. protected override void AddAttributesToRender (HtmlTextWriter w)
  50. {
  51. if (Page != null)
  52. Page.VerifyRenderingInServerForm (this);
  53. base.AddAttributesToRender (w);
  54. if (Page == null)
  55. return;
  56. if (CausesValidation && Page.AreValidatorsUplevel ()) {
  57. ClientScriptManager csm = new ClientScriptManager (Page);
  58. w.AddAttribute (HtmlTextWriterAttribute.Href,
  59. String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0};}}",
  60. csm.GetPostBackEventReference (this, String.Empty)));
  61. w.AddAttribute ("language", "javascript");
  62. } else {
  63. w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  64. }
  65. }
  66. #if NET_2_0
  67. [MonoTODO]
  68. protected virtual void RaisePostBackEvent (string eventArgument)
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. #endif
  73. void IPostBackEventHandler.RaisePostBackEvent (string ea)
  74. {
  75. if (CausesValidation)
  76. #if NET_2_0
  77. Page.Validate (ValidationGroup);
  78. #else
  79. Page.Validate ();
  80. #endif
  81. OnClick (EventArgs.Empty);
  82. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  83. }
  84. protected override void AddParsedSubObject (object obj)
  85. {
  86. if (HasControls ()) {
  87. base.AddParsedSubObject (obj);
  88. return;
  89. }
  90. LiteralControl lc = obj as LiteralControl;
  91. if (lc == null) {
  92. string s = Text;
  93. if (s.Length != 0) {
  94. Text = null;
  95. Controls.Add (new LiteralControl (s));
  96. }
  97. base.AddParsedSubObject (obj);
  98. } else {
  99. Text = lc.Text;
  100. }
  101. }
  102. #if NET_2_0
  103. [MonoTODO]
  104. protected virtual PostBackOptions GetPostBackOptions ()
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. #endif
  109. protected override void LoadViewState (object savedState)
  110. {
  111. base.LoadViewState (savedState);
  112. // Make sure we clear child controls when this happens
  113. if (ViewState ["Text"] != null)
  114. Text = (string) ViewState ["Text"];
  115. }
  116. [MonoTODO ("Why override?")]
  117. #if NET_2_0
  118. protected internal
  119. #else
  120. protected
  121. #endif
  122. override void OnPreRender (EventArgs e)
  123. {
  124. base.OnPreRender (e);
  125. }
  126. #if NET_2_0
  127. protected internal
  128. #else
  129. protected
  130. #endif
  131. override void RenderContents (HtmlTextWriter writer)
  132. {
  133. if (HasControls ())
  134. base.RenderContents (writer);
  135. else
  136. writer.Write (Text);
  137. }
  138. #if ONLY_1_1
  139. [Bindable(false)]
  140. #endif
  141. [DefaultValue(true)]
  142. [WebSysDescription ("")]
  143. [WebCategory ("Behavior")]
  144. #if NET_2_0
  145. [Themeable (false)]
  146. public virtual
  147. #else
  148. public
  149. #endif
  150. bool CausesValidation {
  151. get {
  152. return ViewState.GetBool ("CausesValidation", true);
  153. }
  154. set {
  155. ViewState ["CausesValidation"] = value;
  156. }
  157. }
  158. [Bindable(true)]
  159. [DefaultValue("")]
  160. [WebSysDescription ("")]
  161. [WebCategory ("Behavior")]
  162. #if NET_2_0
  163. [Themeable (false)]
  164. public virtual
  165. #else
  166. public
  167. #endif
  168. string CommandArgument {
  169. get {
  170. return ViewState.GetString ("CommandArgument", "");
  171. }
  172. set {
  173. ViewState ["CommandArgument"] = value;
  174. }
  175. }
  176. [DefaultValue("")]
  177. [WebSysDescription ("")]
  178. [WebCategory ("Behavior")]
  179. #if NET_2_0
  180. [Themeable (false)]
  181. public virtual
  182. #else
  183. public
  184. #endif
  185. 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 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 string ValidationGroup {
  273. get {
  274. return ViewState.GetString ("ValidationGroup", "");
  275. }
  276. set {
  277. ViewState ["ValidationGroup"] = value;
  278. }
  279. }
  280. #endif
  281. }
  282. }