HyperLink.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // System.Web.UI.WebControls.Label.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // TODO: Are we missing something in LoadViewState?
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.WebControls
  33. {
  34. // CAS
  35. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [ControlBuilder(typeof(HyperLinkControlBuilder))]
  39. [DataBindingHandler("System.Web.UI.Design.HyperLinkDataBindingHandler, " + Consts.AssemblySystem_Design)]
  40. [ParseChildren (false)]
  41. [ToolboxData("<{0}:HyperLink runat=\"server\">HyperLink</{0}:HyperLink>")]
  42. [DefaultProperty("Text")]
  43. [Designer("System.Web.UI.Design.WebControls.HyperLinkDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  44. public class HyperLink : WebControl
  45. {
  46. public HyperLink () : base (HtmlTextWriterTag.A)
  47. {
  48. }
  49. protected override void AddAttributesToRender (HtmlTextWriter w)
  50. {
  51. base.AddAttributesToRender (w);
  52. AddDisplayStyleAttribute (w);
  53. if (!IsEnabled)
  54. return;
  55. // add attributes - only if they're not empty
  56. string t = Target;
  57. string s = NavigateUrl;
  58. if (s.Length > 0)
  59. #if TARGET_J2EE
  60. w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (s, String.Compare (t, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0));
  61. #else
  62. w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (s));
  63. #endif
  64. if (t.Length > 0)
  65. w.AddAttribute (HtmlTextWriterAttribute.Target, t);
  66. }
  67. protected override void AddParsedSubObject (object obj)
  68. {
  69. if (HasControls ()) {
  70. base.AddParsedSubObject (obj);
  71. return;
  72. }
  73. LiteralControl lc = obj as LiteralControl;
  74. if (lc == null) {
  75. string s = Text;
  76. if (s.Length != 0) {
  77. Text = null;
  78. Controls.Add (new LiteralControl (s));
  79. }
  80. base.AddParsedSubObject (obj);
  81. } else
  82. Text = lc.Text;
  83. }
  84. [MonoTODO ("Why override?")]
  85. protected override void LoadViewState (object savedState)
  86. {
  87. base.LoadViewState (savedState);
  88. }
  89. protected internal override void RenderContents (HtmlTextWriter w)
  90. {
  91. if (HasControls () || HasRenderMethodDelegate ()) {
  92. base.RenderContents (w);
  93. return;
  94. }
  95. string image_url = ImageUrl;
  96. if (!String.IsNullOrEmpty (image_url)) {
  97. Image img = new Image ();
  98. img.ImageUrl = ResolveClientUrl (image_url);
  99. string str = Text;
  100. if (!String.IsNullOrEmpty (str))
  101. img.AlternateText = str;
  102. // #if NET_4_0
  103. // else
  104. // img.GenerateEmptyAlternateText = true;
  105. // #else
  106. // img.BorderWidth = 0;
  107. // #endif
  108. str = ToolTip;
  109. if (!String.IsNullOrEmpty (str))
  110. img.ToolTip = str;
  111. img.RenderControl (w);
  112. } else
  113. w.Write (Text);
  114. }
  115. [Bindable(true)]
  116. [DefaultValue("")]
  117. [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  118. [UrlProperty]
  119. [WebSysDescription ("")]
  120. [WebCategory ("Appearance")]
  121. public virtual string ImageUrl {
  122. get { return ViewState.GetString ("ImageUrl", String.Empty); }
  123. set { ViewState ["ImageUrl"] = value; }
  124. }
  125. [Bindable(true)]
  126. [DefaultValue("")]
  127. [Editor("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  128. [UrlProperty]
  129. [WebSysDescription ("")]
  130. [WebCategory ("Navigation")]
  131. public string NavigateUrl {
  132. get { return ViewState.GetString ("NavigateUrl", String.Empty); }
  133. set { ViewState ["NavigateUrl"] = value; }
  134. }
  135. [DefaultValue("")]
  136. [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
  137. [WebSysDescription ("")]
  138. [WebCategory ("Navigation")]
  139. public string Target {
  140. get { return ViewState.GetString ("Target", String.Empty); }
  141. set { ViewState ["Target"] = value; }
  142. }
  143. [Bindable(true)]
  144. [DefaultValue("")]
  145. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  146. [Localizable (true)]
  147. [WebSysDescription ("")]
  148. [WebCategory ("Appearance")]
  149. public virtual string Text {
  150. get { return ViewState.GetString ("Text", String.Empty); }
  151. set {
  152. ViewState ["Text"] = value;
  153. if (HasControls ())
  154. Controls.Clear ();
  155. }
  156. }
  157. #if NET_4_0
  158. public override bool SupportsDisabledAttribute {
  159. get { return RenderingCompatibilityLessThan40; }
  160. }
  161. #endif
  162. }
  163. }