HtmlAnchor.cs 4.4 KB

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