svn path=/branches/mono-1-1-9/mcs/; revision=51212
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HyperLink.cs
1 //
2 // System.Web.UI.WebControls.Label.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 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
31 using System.ComponentModel;
32 using System.Web.UI;
33
34 namespace System.Web.UI.WebControls {
35         [ControlBuilder(typeof(HyperLinkControlBuilder))]
36         [DataBindingHandler("System.Web.UI.Design.HyperLinkDataBindingHandler, " + Consts.AssemblySystem_Design)]
37         [ParseChildren (false)]
38 #if NET_2_0
39         [ToolboxData("<{0}:HyperLink runat=\"server\">HyperLink</{0}:HyperLink>")]
40 #else           
41         [ToolboxData("<{0}:HyperLink runat=server>HyperLink</{0}:HyperLink>")]
42 #endif          
43         [DefaultProperty("Text")]
44         [Designer("System.Web.UI.Design.WebControls.HyperLinkDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         public class HyperLink : WebControl {
46
47                 public HyperLink () : base (HtmlTextWriterTag.A)
48                 {
49                 }
50                                 
51                 protected override void AddAttributesToRender (HtmlTextWriter w)
52                 {
53                         base.AddAttributesToRender (w);
54                         // add attributes - only if they're not empty
55                         string s = NavigateUrl;
56                         if (s.Length > 0)
57                                 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveUrl (s));
58                         s = Target;
59                         if (s.Length > 0)
60                                 w.AddAttribute (HtmlTextWriterAttribute.Target, s);
61                 }
62                 
63                 protected override void AddParsedSubObject (object obj)
64                 {
65                         if (HasControls ()) {
66                                 base.AddParsedSubObject (obj);
67                                 return;
68                         }
69                         
70                         LiteralControl lc = obj as LiteralControl;
71
72                         if (lc == null) {
73                                 string s = Text;
74                                 if (s.Length != 0) {
75                                         Text = null;
76                                         Controls.Add (new LiteralControl (s));
77                                 }
78                                 base.AddParsedSubObject (obj);
79                         } else {
80                                 Text = lc.Text;
81                         }
82                 }
83                 
84
85                 [MonoTODO ("Why override?")]
86                 protected override void LoadViewState (object savedState)
87                 {
88                         base.LoadViewState (savedState);
89                 }
90                 
91 #if NET_2_0
92                 protected internal
93 #else           
94                 protected
95 #endif          
96                 override void RenderContents (HtmlTextWriter w) 
97                 {
98                         if (HasControls ())
99                                 base.RenderContents (w);
100                         else if (ImageUrl != "") {
101                                 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));
102 #if NET_2_0
103                                 string s = Text;
104                                 if (s.Length > 0)
105                                         w.AddAttribute (HtmlTextWriterAttribute.Alt, s);
106                                 w.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
107 #else
108                                 w.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
109                                 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
110 #endif
111                                 w.RenderBeginTag (HtmlTextWriterTag.Img);
112                                 w.RenderEndTag ();
113                         } else {
114                                 w.Write (Text);
115                         }
116                         
117                 }
118
119                 [Bindable(true)]
120                 [DefaultValue("")]
121                 [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
122 #if NET_2_0
123                 [UrlProperty]
124 #endif          
125                 [WebSysDescription ("")]
126                 [WebCategory ("Appearance")]
127                 public virtual string ImageUrl {
128                         get {
129                                 return ViewState.GetString ("ImageUrl", "");
130                         }
131                         set {
132                                 ViewState ["ImageUrl"] = value;
133                         }       
134                 }
135                 
136                 [Bindable(true)]
137                 [DefaultValue("")]
138                 [Editor("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
139 #if NET_2_0
140                 [UrlProperty]
141 #endif          
142                 [WebSysDescription ("")]
143                 [WebCategory ("Navigation")]
144                 public string NavigateUrl {
145                         get {
146                                 return ViewState.GetString ("NavigateUrl", "");
147                         }
148                         set {
149                                 ViewState ["NavigateUrl"] = value;
150                         }       
151                 }
152                 
153 #if ONLY_1_1
154                 [Bindable(true)]
155 #endif          
156                 [DefaultValue("")]
157                 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
158                 [WebSysDescription ("")]
159                 [WebCategory ("Navigation")]
160                 public string Target {
161                         get {
162                                 return ViewState.GetString ("Target", "");
163                         }
164                         set {
165                                 ViewState ["Target"] = value;
166                         }
167                 }
168                 
169                 [Bindable(true)]
170                 [DefaultValue("")]
171                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
172 #if NET_2_0
173                 [Localizable (true)]
174 #endif          
175                 [WebSysDescription ("")]
176                 [WebCategory ("Appearance")]
177                 public virtual string Text {
178                         get {
179                                 return ViewState.GetString ("Text", "");
180                         }
181                         set {
182                                 ViewState ["Text"] = value;
183                                 if (HasControls ())
184                                         Controls.Clear ();
185                         }
186                 }
187         }
188 }