HtmlAnchor.cs 6.1 KB

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