HtmlAnchor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. protected internal
  118. #else
  119. protected
  120. #endif
  121. override void OnPreRender (EventArgs e)
  122. {
  123. base.OnPreRender (e);
  124. }
  125. protected virtual void OnServerClick (EventArgs e)
  126. {
  127. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  128. if (serverClick != null)
  129. serverClick (this, e);
  130. }
  131. protected override void RenderAttributes (HtmlTextWriter writer)
  132. {
  133. // we don't want to render the "user" URL, so we either render:
  134. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  135. if (serverClick != null) {
  136. // a script
  137. ClientScriptManager csm = new ClientScriptManager (Page);
  138. Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
  139. } else {
  140. string hr = HRef;
  141. if (hr != "")
  142. HRef = ResolveUrl (hr);
  143. }
  144. base.RenderAttributes (writer);
  145. // but we never set back the href attribute after the rendering
  146. // nor is the property available after rendering
  147. Attributes.Remove ("href");
  148. }
  149. #if NET_2_0
  150. protected virtual void RaisePostBackEvent (string eventArgument)
  151. {
  152. OnServerClick (EventArgs.Empty);
  153. }
  154. #endif
  155. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  156. {
  157. #if NET_2_0
  158. RaisePostBackEvent (eventArgument);
  159. #else
  160. OnServerClick (EventArgs.Empty);
  161. #endif
  162. }
  163. [WebSysDescription("")]
  164. [WebCategory("Action")]
  165. public event EventHandler ServerClick {
  166. add { Events.AddHandler (serverClickEvent, value); }
  167. remove { Events.RemoveHandler (serverClickEvent, value); }
  168. }
  169. }
  170. }