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. string str = ToolTip;
  98. if (!String.IsNullOrEmpty (str))
  99. w.AddAttribute (HtmlTextWriterAttribute.Title, str);
  100. w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (image_url));
  101. str = Text;
  102. #if !NET_4_0
  103. if (!String.IsNullOrEmpty (str))
  104. #endif
  105. w.AddAttribute (HtmlTextWriterAttribute.Alt, str);
  106. #if !NET_4_0
  107. w.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
  108. #endif
  109. w.RenderBeginTag (HtmlTextWriterTag.Img);
  110. w.RenderEndTag ();
  111. } else
  112. w.Write (Text);
  113. }
  114. [Bindable(true)]
  115. [DefaultValue("")]
  116. [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  117. [UrlProperty]
  118. [WebSysDescription ("")]
  119. [WebCategory ("Appearance")]
  120. public virtual string ImageUrl {
  121. get { return ViewState.GetString ("ImageUrl", String.Empty); }
  122. set { ViewState ["ImageUrl"] = value; }
  123. }
  124. [Bindable(true)]
  125. [DefaultValue("")]
  126. [Editor("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  127. [UrlProperty]
  128. [WebSysDescription ("")]
  129. [WebCategory ("Navigation")]
  130. public string NavigateUrl {
  131. get { return ViewState.GetString ("NavigateUrl", String.Empty); }
  132. set { ViewState ["NavigateUrl"] = value; }
  133. }
  134. [DefaultValue("")]
  135. [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
  136. [WebSysDescription ("")]
  137. [WebCategory ("Navigation")]
  138. public string Target {
  139. get { return ViewState.GetString ("Target", String.Empty); }
  140. set { ViewState ["Target"] = value; }
  141. }
  142. [Bindable(true)]
  143. [DefaultValue("")]
  144. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  145. [Localizable (true)]
  146. [WebSysDescription ("")]
  147. [WebCategory ("Appearance")]
  148. public virtual string Text {
  149. get { return ViewState.GetString ("Text", String.Empty); }
  150. set {
  151. ViewState ["Text"] = value;
  152. if (HasControls ())
  153. Controls.Clear ();
  154. }
  155. }
  156. #if NET_4_0
  157. public override bool SupportsDisabledAttribute {
  158. get { return RenderingCompatibilityLessThan40; }
  159. }
  160. #endif
  161. }
  162. }