LinkButton.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #if NET_2_0
  60. string onclick = OnClientClick;
  61. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
  62. if (Attributes ["onclick"] != null) {
  63. onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
  64. Attributes.Remove ("onclick");
  65. }
  66. if (onclick.Length > 0)
  67. w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
  68. if (Enabled && Page != null) {
  69. PostBackOptions options = GetPostBackOptions ();
  70. string href = Page.ClientScript.GetPostBackEventReference (options, true);
  71. w.AddAttribute (HtmlTextWriterAttribute.Href, href);
  72. }
  73. base.AddAttributesToRender (w);
  74. AddDisplayStyleAttribute (w);
  75. #else
  76. base.AddAttributesToRender (w);
  77. if (Page == null || !Enabled)
  78. return;
  79. if (CausesValidation && Page.AreValidatorsUplevel ()) {
  80. ClientScriptManager csm = new ClientScriptManager (Page);
  81. w.AddAttribute (HtmlTextWriterAttribute.Href,
  82. String.Concat ("javascript:{if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); ",
  83. csm.GetPostBackEventReference (this, String.Empty), ";}"));
  84. } else {
  85. w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  86. }
  87. #endif
  88. }
  89. #if NET_2_0
  90. protected virtual
  91. #endif
  92. void RaisePostBackEvent (string eventArgument)
  93. {
  94. if (CausesValidation)
  95. #if NET_2_0
  96. Page.Validate (ValidationGroup);
  97. #else
  98. Page.Validate ();
  99. #endif
  100. OnClick (EventArgs.Empty);
  101. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  102. }
  103. void IPostBackEventHandler.RaisePostBackEvent (string ea)
  104. {
  105. RaisePostBackEvent (ea);
  106. }
  107. protected override void AddParsedSubObject (object obj)
  108. {
  109. if (HasControls ()) {
  110. base.AddParsedSubObject (obj);
  111. return;
  112. }
  113. LiteralControl lc = obj as LiteralControl;
  114. if (lc == null) {
  115. string s = Text;
  116. if (s.Length != 0) {
  117. Text = null;
  118. Controls.Add (new LiteralControl (s));
  119. }
  120. base.AddParsedSubObject (obj);
  121. } else {
  122. Text = lc.Text;
  123. }
  124. }
  125. #if NET_2_0
  126. protected virtual PostBackOptions GetPostBackOptions ()
  127. {
  128. PostBackOptions options = new PostBackOptions (this);
  129. options.ActionUrl = (PostBackUrl.Length > 0 ? Page.ResolveClientUrl (PostBackUrl) : null);
  130. options.ValidationGroup = null;
  131. options.Argument = "";
  132. options.ClientSubmit = true;
  133. options.RequiresJavaScriptProtocol = true;
  134. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  135. if (options.PerformValidation)
  136. options.ValidationGroup = ValidationGroup;
  137. return options;
  138. }
  139. #endif
  140. protected override void LoadViewState (object savedState)
  141. {
  142. base.LoadViewState (savedState);
  143. // Make sure we clear child controls when this happens
  144. if (ViewState ["Text"] != null)
  145. Text = (string) ViewState ["Text"];
  146. }
  147. [MonoTODO ("Why override?")]
  148. #if NET_2_0
  149. protected internal
  150. #else
  151. protected
  152. #endif
  153. override void OnPreRender (EventArgs e)
  154. {
  155. base.OnPreRender (e);
  156. }
  157. #if NET_2_0
  158. protected internal
  159. #else
  160. protected
  161. #endif
  162. override void RenderContents (HtmlTextWriter writer)
  163. {
  164. if (HasControls () || HasRenderMethodDelegate ())
  165. base.RenderContents (writer);
  166. else
  167. writer.Write (Text);
  168. }
  169. #if ONLY_1_1
  170. [Bindable(false)]
  171. #endif
  172. [DefaultValue(true)]
  173. [WebSysDescription ("")]
  174. [WebCategory ("Behavior")]
  175. #if NET_2_0
  176. [Themeable (false)]
  177. public virtual
  178. #else
  179. public
  180. #endif
  181. bool CausesValidation {
  182. get {
  183. return ViewState.GetBool ("CausesValidation", true);
  184. }
  185. set {
  186. ViewState ["CausesValidation"] = value;
  187. }
  188. }
  189. [Bindable(true)]
  190. [DefaultValue("")]
  191. [WebSysDescription ("")]
  192. [WebCategory ("Behavior")]
  193. #if NET_2_0
  194. [Themeable (false)]
  195. #endif
  196. public string CommandArgument {
  197. get {
  198. return ViewState.GetString ("CommandArgument", "");
  199. }
  200. set {
  201. ViewState ["CommandArgument"] = value;
  202. }
  203. }
  204. [DefaultValue("")]
  205. [WebSysDescription ("")]
  206. [WebCategory ("Behavior")]
  207. #if NET_2_0
  208. [Themeable (false)]
  209. #endif
  210. public string CommandName {
  211. get {
  212. return ViewState.GetString ("CommandName", "");
  213. }
  214. set {
  215. ViewState ["CommandName"] = value;
  216. }
  217. }
  218. #if NET_2_0
  219. [DefaultValue ("")]
  220. [Themeable (false)]
  221. [WebSysDescription ("")]
  222. [WebCategoryAttribute ("Behavior")]
  223. public virtual string OnClientClick
  224. {
  225. get {
  226. return ViewState.GetString ("OnClientClick", "");
  227. }
  228. set {
  229. ViewState ["OnClientClick"] = value;
  230. }
  231. }
  232. #endif
  233. [Bindable(true)]
  234. [DefaultValue("")]
  235. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  236. #if NET_2_0
  237. [Localizable (true)]
  238. #endif
  239. [WebSysDescription ("")]
  240. [WebCategory ("Appearance")]
  241. public virtual string Text {
  242. get {
  243. return ViewState.GetString ("Text", "");
  244. }
  245. set {
  246. ViewState ["Text"] = value;
  247. if (HasControls ())
  248. Controls.Clear ();
  249. }
  250. }
  251. protected virtual void OnClick (EventArgs e)
  252. {
  253. EventHandler h = (EventHandler) Events [ClickEvent];
  254. if (h != null)
  255. h (this, e);
  256. }
  257. static readonly object ClickEvent = new object ();
  258. [WebSysDescription ("")]
  259. [WebCategory ("Action")]
  260. public event EventHandler Click {
  261. add { Events.AddHandler (ClickEvent, value); }
  262. remove { Events.RemoveHandler (ClickEvent, value); }
  263. }
  264. protected virtual void OnCommand (CommandEventArgs e)
  265. {
  266. CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
  267. if (h != null)
  268. h (this, e);
  269. RaiseBubbleEvent (this, e);
  270. }
  271. static readonly object CommandEvent = new object ();
  272. [WebSysDescription ("")]
  273. [WebCategory ("Action")]
  274. public event CommandEventHandler Command {
  275. add { Events.AddHandler (CommandEvent, value); }
  276. remove { Events.RemoveHandler (CommandEvent, value); }
  277. }
  278. #if NET_2_0
  279. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  280. [Themeable (false)]
  281. [UrlProperty ("*.aspx")]
  282. [DefaultValue ("")]
  283. public virtual string PostBackUrl {
  284. get {
  285. return ViewState.GetString ("PostBackUrl", String.Empty);
  286. }
  287. set {
  288. ViewState["PostBackUrl"] = value;
  289. }
  290. }
  291. [DefaultValue ("")]
  292. [Themeable (false)]
  293. [WebSysDescription ("")]
  294. [WebCategoryAttribute ("Behavior")]
  295. public virtual string ValidationGroup {
  296. get {
  297. return ViewState.GetString ("ValidationGroup", "");
  298. }
  299. set {
  300. ViewState ["ValidationGroup"] = value;
  301. }
  302. }
  303. #endif
  304. }
  305. }