HtmlAnchor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. public class HtmlAnchor : HtmlContainerControl, IPostBackEventHandler {
  37. private static readonly object serverClickEvent = new object ();
  38. public HtmlAnchor ()
  39. : base ("a")
  40. {
  41. }
  42. [DefaultValue ("")]
  43. [WebSysDescription("")]
  44. [WebCategory("Action")]
  45. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  46. public string HRef {
  47. get {
  48. string s = Attributes ["href"];
  49. return (s == null) ? String.Empty : s;
  50. }
  51. set {
  52. if (value == null) {
  53. Attributes.Remove ("href");
  54. } else {
  55. Attributes ["href"] = value;
  56. }
  57. }
  58. }
  59. [DefaultValue ("")]
  60. [WebSysDescription("")]
  61. [WebCategory("Navigation")]
  62. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  63. public string Name {
  64. get {
  65. string s = Attributes ["name"];
  66. return (s == null) ? String.Empty : s;
  67. }
  68. set {
  69. if (value == null)
  70. Attributes.Remove ("name");
  71. else
  72. Attributes ["name"] = value;
  73. }
  74. }
  75. [DefaultValue ("")]
  76. [WebSysDescription("")]
  77. [WebCategory("Navigation")]
  78. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  79. public string Target {
  80. get {
  81. string s = Attributes ["target"];
  82. return (s == null) ? String.Empty : s;
  83. }
  84. set {
  85. if (value == null)
  86. Attributes.Remove ("target");
  87. else
  88. Attributes ["target"] = value;
  89. }
  90. }
  91. [DefaultValue ("")]
  92. [WebSysDescription("")]
  93. [WebCategory("Appearance")]
  94. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  95. #if NET_2_0
  96. [Localizable (true)]
  97. #endif
  98. public string Title {
  99. get {
  100. string s = Attributes ["title"];
  101. return (s == null) ? String.Empty : s;
  102. }
  103. set {
  104. if (value == null)
  105. Attributes.Remove ("title");
  106. else
  107. Attributes ["title"] = value;
  108. }
  109. }
  110. #if NET_2_0
  111. protected internal
  112. #else
  113. protected
  114. #endif
  115. override void OnPreRender (EventArgs e)
  116. {
  117. base.OnPreRender (e);
  118. }
  119. protected virtual void OnServerClick (EventArgs e)
  120. {
  121. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  122. if (serverClick != null)
  123. serverClick (this, e);
  124. }
  125. protected override void RenderAttributes (HtmlTextWriter writer)
  126. {
  127. // we don't want to render the "user" URL, so we either render:
  128. EventHandler serverClick = (EventHandler) Events [serverClickEvent];
  129. if (serverClick != null) {
  130. // a script
  131. ClientScriptManager csm = new ClientScriptManager (Page);
  132. Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
  133. } else {
  134. string hr = HRef;
  135. if (hr != "")
  136. HRef = ResolveUrl (hr);
  137. }
  138. base.RenderAttributes (writer);
  139. // but we never set back the href attribute after the rendering
  140. // nor is the property available after rendering
  141. Attributes.Remove ("href");
  142. }
  143. #if NET_2_0
  144. protected virtual void RaisePostBackEvent (string eventArgument)
  145. {
  146. OnServerClick (EventArgs.Empty);
  147. }
  148. #endif
  149. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  150. {
  151. #if NET_2_0
  152. RaisePostBackEvent (eventArgument);
  153. #else
  154. OnServerClick (EventArgs.Empty);
  155. #endif
  156. }
  157. [WebSysDescription("")]
  158. [WebCategory("Action")]
  159. public event EventHandler ServerClick {
  160. add { Events.AddHandler (serverClickEvent, value); }
  161. remove { Events.RemoveHandler (serverClickEvent, value); }
  162. }
  163. }
  164. }