HtmlAnchor.cs 6.2 KB

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