HyperLink.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // System.Web.UI.WebControls.HyperLink.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. // (C) Gaurav Vaish (2002)
  11. // (C) 2003 Andreas Nahr
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.ComponentModel;
  37. using System.ComponentModel.Design;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [DefaultProperty("Text")]
  41. [ControlBuilder(typeof(HyperLinkControlBuilder))]
  42. [Designer("System.Web.UI.Design.WebControls.HyperLinkDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  43. [DataBindingHandler("System.Web.UI.Design.HyperLinkDataBindingHandler, " + Consts.AssemblySystem_Design)]
  44. [ParseChildren(false)]
  45. [ToolboxData("<{0}:HyperLink runat=\"server\">HyperLink</{0}:HyperLink>")]
  46. public class HyperLink: WebControl
  47. {
  48. bool textSet;
  49. public HyperLink(): base(HtmlTextWriterTag.A)
  50. {
  51. }
  52. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  53. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  54. [WebSysDescription ("The URL to the image file.")]
  55. public virtual string ImageUrl
  56. {
  57. get
  58. {
  59. object o = ViewState["ImageUrl"];
  60. if(o!=null)
  61. return (string)o;
  62. return String.Empty;
  63. }
  64. set
  65. {
  66. ViewState["ImageUrl"] = value;
  67. }
  68. }
  69. [DefaultValue (""), Bindable (true), WebCategory ("Navigation")]
  70. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  71. [WebSysDescription ("The URL to navigate to.")]
  72. public string NavigateUrl
  73. {
  74. get
  75. {
  76. object o = ViewState["NavigateUrl"];
  77. if(o!=null)
  78. return (string)o;
  79. return String.Empty;
  80. }
  81. set
  82. {
  83. ViewState["NavigateUrl"] = value;
  84. }
  85. }
  86. [DefaultValue (""), Bindable (true), WebCategory ("Navigation")]
  87. [TypeConverter (typeof (TargetConverter))]
  88. [WebSysDescription ("The target frame in which the navigation target should be opened.")]
  89. public string Target
  90. {
  91. get
  92. {
  93. object o = ViewState["Target"];
  94. if(o!=null)
  95. return (string)o;
  96. return String.Empty;
  97. }
  98. set
  99. {
  100. ViewState["Target"] = value;
  101. }
  102. }
  103. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  104. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  105. [WebSysDescription ("The text that should be shown on this HyperLink.")]
  106. public virtual string Text
  107. {
  108. get {
  109. object o = ViewState ["Text"];
  110. if (o != null)
  111. return (string) o;
  112. return String.Empty;
  113. }
  114. set {
  115. if (HasControls())
  116. Controls.Clear();
  117. ViewState["Text"] = value;
  118. textSet = true;
  119. }
  120. }
  121. string InternalText
  122. {
  123. get { return Text; }
  124. set { ViewState["Text"] = value; }
  125. }
  126. protected override void AddAttributesToRender(HtmlTextWriter writer)
  127. {
  128. base.AddAttributesToRender(writer);
  129. if(NavigateUrl.Length > 0)
  130. {
  131. string url = ResolveUrl (NavigateUrl);
  132. writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
  133. }
  134. if(Target.Length > 0)
  135. {
  136. writer.AddAttribute(HtmlTextWriterAttribute.Target, Target);
  137. }
  138. }
  139. protected override void AddParsedSubObject(object obj)
  140. {
  141. if(HasControls())
  142. {
  143. base.AddParsedSubObject(obj);
  144. return;
  145. }
  146. if(obj is LiteralControl)
  147. {
  148. // This is a hack to workaround the behaviour of the code generator, which
  149. // may split a text in several LiteralControls if there's a special character
  150. // such as '<' in it.
  151. if (textSet) {
  152. Text = ((LiteralControl)obj).Text;
  153. textSet = false;
  154. } else {
  155. InternalText += ((LiteralControl)obj).Text;
  156. }
  157. //
  158. return;
  159. }
  160. if(Text.Length > 0)
  161. {
  162. base.AddParsedSubObject(new LiteralControl (Text));
  163. Text = String.Empty;
  164. }
  165. base.AddParsedSubObject (obj);
  166. }
  167. protected override void LoadViewState(object savedState)
  168. {
  169. if(savedState != null)
  170. {
  171. base.LoadViewState(savedState);
  172. object o = ViewState["Text"];
  173. if(o!=null)
  174. Text = (string)o;
  175. }
  176. }
  177. protected override void RenderContents(HtmlTextWriter writer)
  178. {
  179. if(ImageUrl.Length > 0)
  180. {
  181. Image img = new Image();
  182. img.ImageUrl = ResolveUrl(ImageUrl);
  183. if(ToolTip.Length > 0)
  184. img.ToolTip = ToolTip;
  185. if(Text.Length > 0)
  186. img.AlternateText = Text;
  187. img.RenderControl(writer);
  188. return;
  189. }
  190. if(HasControls())
  191. {
  192. base.RenderContents(writer);
  193. return;
  194. }
  195. writer.Write(Text);
  196. }
  197. }
  198. }