LinkButton.cs 8.7 KB

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