HtmlAnchor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // System.Web.UI.HtmlControls.HtmlAnchor.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (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.HtmlControls {
  31. // CAS
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [DefaultEvent ("ServerClick")]
  36. #if NET_2_0
  37. [SupportsEventValidation]
  38. #endif
  39. public class HtmlAnchor : HtmlContainerControl, IPostBackEventHandler
  40. {
  41. static readonly object serverClickEvent = new object ();
  42. public HtmlAnchor ()
  43. : base ("a")
  44. {
  45. }
  46. [DefaultValue ("")]
  47. [WebSysDescription("")]
  48. [WebCategory("Action")]
  49. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  50. #if NET_2_0
  51. [UrlProperty]
  52. #endif
  53. public string HRef {
  54. get {
  55. string s = Attributes ["href"];
  56. return (s == null) ? String.Empty : s;
  57. }
  58. set {
  59. if (value == null || value.Length == 0) {
  60. Attributes.Remove ("href");
  61. } else {
  62. Attributes ["href"] = value;
  63. }
  64. }
  65. }
  66. [DefaultValue ("")]
  67. [WebSysDescription("")]
  68. [WebCategory("Navigation")]
  69. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  70. public string Name {
  71. get {
  72. string s = Attributes ["name"];
  73. return (s == null) ? String.Empty : s;
  74. }
  75. set {
  76. if (value == null || value.Length == 0)
  77. Attributes.Remove ("name");
  78. else
  79. Attributes ["name"] = value;
  80. }
  81. }
  82. [DefaultValue ("")]
  83. [WebSysDescription("")]
  84. [WebCategory("Navigation")]
  85. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  86. public string Target {
  87. get {
  88. string s = Attributes ["target"];
  89. return (s == null) ? String.Empty : s;
  90. }
  91. set {
  92. if (value == null || value.Length == 0)
  93. Attributes.Remove ("target");
  94. else
  95. Attributes ["target"] = value;
  96. }
  97. }
  98. [DefaultValue ("")]
  99. [WebSysDescription("")]
  100. [WebCategory("Appearance")]
  101. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  102. #if NET_2_0
  103. [Localizable (true)]
  104. #endif
  105. public string Title {
  106. get {
  107. string s = Attributes ["title"];
  108. return (s == null) ? String.Empty : s;
  109. }
  110. set {
  111. if (value == null || value.Length == 0)
  112. Attributes.Remove ("title");
  113. else
  114. Attributes ["title"] = value;
  115. }
  116. }
  117. #if NET_2_0
  118. [DefaultValue (true)]
  119. public virtual bool CausesValidation {
  120. get {
  121. return ViewState.GetBool ("CausesValidation", true);
  122. }
  123. set {
  124. ViewState ["CausesValidation"] = value;
  125. }
  126. }
  127. [DefaultValue ("")]
  128. public virtual string ValidationGroup {
  129. get {
  130. return ViewState.GetString ("ValidationGroup", String.Empty);
  131. }
  132. set {
  133. ViewState ["ValidationGroup"] = value;
  134. }
  135. }
  136. #endif
  137. #if NET_2_0
  138. protected internal
  139. #else
  140. protected
  141. #endif
  142. override void OnPreRender (EventArgs e)
  143. {
  144. base.OnPreRender (e);
  145. }
  146. protected virtual void OnServerClick (EventArgs e)
  147. {
  148. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  149. if (serverClick != null)
  150. serverClick (this, e);
  151. }
  152. protected override void RenderAttributes (HtmlTextWriter writer)
  153. {
  154. // we don't want to render the "user" URL, so we either render:
  155. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  156. if (serverClick != null) {
  157. ClientScriptManager csm;
  158. #if NET_2_0
  159. // a script
  160. PostBackOptions options = GetPostBackOptions ();
  161. csm = Page.ClientScript;
  162. csm.RegisterForEventValidation (options);
  163. Attributes ["href"] = csm.GetPostBackEventReference (options, true);
  164. #else
  165. // a script
  166. csm = new ClientScriptManager (Page);
  167. Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
  168. #endif
  169. } else {
  170. string hr = HRef;
  171. if (hr != string.Empty)
  172. #if TARGET_J2EE
  173. // For J2EE portlets we need to genreate a render URL.
  174. HRef = ResolveClientUrl (hr, String.Compare (Target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
  175. #else
  176. HRef = ResolveClientUrl (hr);
  177. #endif
  178. }
  179. base.RenderAttributes (writer);
  180. // but we never set back the href attribute after the rendering
  181. // nor is the property available after rendering
  182. Attributes.Remove ("href");
  183. }
  184. #if NET_2_0
  185. protected virtual void RaisePostBackEvent (string eventArgument)
  186. {
  187. ValidateEvent (UniqueID, eventArgument);
  188. if (CausesValidation)
  189. Page.Validate (ValidationGroup);
  190. OnServerClick (EventArgs.Empty);
  191. }
  192. PostBackOptions GetPostBackOptions ()
  193. {
  194. PostBackOptions options = new PostBackOptions (this);
  195. options.ValidationGroup = null;
  196. options.ActionUrl = null;
  197. options.Argument = String.Empty;
  198. options.RequiresJavaScriptProtocol = true;
  199. options.ClientSubmit = true;
  200. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  201. if (options.PerformValidation)
  202. options.ValidationGroup = ValidationGroup;
  203. return options;
  204. }
  205. #endif
  206. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  207. {
  208. #if NET_2_0
  209. RaisePostBackEvent (eventArgument);
  210. #else
  211. OnServerClick (EventArgs.Empty);
  212. #endif
  213. }
  214. [WebSysDescription("")]
  215. [WebCategory("Action")]
  216. public event EventHandler ServerClick {
  217. add { Events.AddHandler (serverClickEvent, value); }
  218. remove { Events.RemoveHandler (serverClickEvent, value); }
  219. }
  220. }
  221. }