HtmlAnchor.cs 4.3 KB

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