2008-11-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlAnchor.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlAnchor.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
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
29 using System.ComponentModel;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.HtmlControls {
33
34         // CAS
35         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [DefaultEvent ("ServerClick")]
39 #if NET_2_0
40         [SupportsEventValidation]
41 #endif
42         public class HtmlAnchor : HtmlContainerControl, IPostBackEventHandler 
43         {
44                 static readonly object serverClickEvent = new object ();
45
46                 public HtmlAnchor ()
47                         : base ("a")
48                 {
49                 }
50
51                 [DefaultValue ("")]
52                 [WebSysDescription("")]
53                 [WebCategory("Action")]
54                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
55 #if NET_2_0
56                 [UrlProperty]
57 #endif
58                 public string HRef {
59                         get {
60                                 string s = Attributes ["href"];
61                                 return (s == null) ? String.Empty : s;
62                         }
63                         set {
64                                 if (value == null || value.Length == 0) {
65                                         Attributes.Remove ("href");
66                                 } else {
67                                         Attributes ["href"] = value;
68                                 }
69                         }
70                 }
71
72                 [DefaultValue ("")]
73                 [WebSysDescription("")]
74                 [WebCategory("Navigation")]
75                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
76                 public string Name {
77                         get {
78                                 string s = Attributes ["name"];
79                                 return (s == null) ? String.Empty : s;
80                         }
81                         set {
82                                 if (value == null || value.Length == 0)
83                                         Attributes.Remove ("name");
84                                 else
85                                         Attributes ["name"] = value;
86                         }
87                 }
88
89                 [DefaultValue ("")]
90                 [WebSysDescription("")]
91                 [WebCategory("Navigation")]
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 public string Target {
94                         get {
95                                 string s = Attributes ["target"];
96                                 return (s == null) ? String.Empty : s;
97                         }
98                         set {
99                                 if (value == null || value.Length == 0)
100                                         Attributes.Remove ("target");
101                                 else
102                                         Attributes ["target"] = value;
103                         }
104                 }
105
106                 [DefaultValue ("")]
107                 [WebSysDescription("")]
108                 [WebCategory("Appearance")]
109                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
110 #if NET_2_0
111                 [Localizable (true)]
112 #endif
113                 public string Title {
114                         get {
115                                 string s = Attributes ["title"];
116                                 return (s == null) ? String.Empty : s;
117                         }
118                         set {
119                                 if (value == null || value.Length == 0)
120                                         Attributes.Remove ("title");
121                                 else
122                                         Attributes ["title"] = value;
123                         }
124                 }
125
126 #if NET_2_0
127                 [DefaultValue (true)]
128                 public virtual bool CausesValidation {
129                         get {
130                                 return ViewState.GetBool ("CausesValidation", true);
131                         }
132                         set {
133                                 ViewState ["CausesValidation"] = value;
134                         }
135                 }
136
137                 [DefaultValue ("")]
138                 public virtual string ValidationGroup {
139                         get {
140                                 return ViewState.GetString ("ValidationGroup", String.Empty);
141                         }
142                         set {
143                                 ViewState ["ValidationGroup"] = value;
144                         }
145                 }
146 #endif
147
148 #if NET_2_0
149                 protected internal
150 #else
151                 protected
152 #endif          
153                 override void OnPreRender (EventArgs e)
154                 {
155                         base.OnPreRender (e);
156                 }
157
158                 protected virtual void OnServerClick (EventArgs e)
159                 {
160                         EventHandler serverClick = (EventHandler) Events [serverClickEvent];
161                         if (serverClick != null)
162                                 serverClick (this, e);
163                 }
164
165                 protected override void RenderAttributes (HtmlTextWriter writer)
166                 {
167                         // we don't want to render the "user" URL, so we either render:
168                         EventHandler serverClick = (EventHandler) Events [serverClickEvent];
169                         if (serverClick != null) {
170                                 ClientScriptManager csm;
171 #if NET_2_0
172                                 // a script
173                                 PostBackOptions options = GetPostBackOptions ();
174                                 csm = Page.ClientScript;
175                                 csm.RegisterForEventValidation (options);
176                                 Attributes ["href"] = csm.GetPostBackEventReference (options, true);
177 #else
178                                 // a script
179                                 csm = new ClientScriptManager (Page);
180                                 Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
181 #endif
182                         } else {
183                                 string hr = HRef;
184                                 if (hr != string.Empty)
185 #if TARGET_J2EE
186                                         // For J2EE portlets we need to genreate a render URL.
187                                         HRef = ResolveClientUrl (hr, String.Compare (Target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
188 #else
189                                         HRef = ResolveClientUrl (hr);
190 #endif
191                         }
192
193                         base.RenderAttributes (writer);
194
195                         // but we never set back the href attribute after the rendering
196                         // nor is the property available after rendering
197                         Attributes.Remove ("href");
198                 }
199
200 #if NET_2_0
201                 protected virtual void RaisePostBackEvent (string eventArgument)
202                 {
203                         ValidateEvent (UniqueID, eventArgument);
204                         if (CausesValidation)
205                                 Page.Validate (ValidationGroup);
206                         
207                         OnServerClick (EventArgs.Empty);
208                 }
209         
210                 PostBackOptions GetPostBackOptions ()
211                 {
212                         PostBackOptions options = new PostBackOptions (this);
213                         options.ValidationGroup = null;
214                         options.ActionUrl = null;
215                         options.Argument = String.Empty;
216                         options.RequiresJavaScriptProtocol = true;
217                         options.ClientSubmit = true;
218                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
219                         if (options.PerformValidation)
220                                 options.ValidationGroup = ValidationGroup;
221
222                         return options;
223                 }
224 #endif
225
226                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
227                 {
228 #if NET_2_0
229                         RaisePostBackEvent (eventArgument);
230 #else
231                         OnServerClick (EventArgs.Empty);
232 #endif
233                 }
234
235                 [WebSysDescription("")]
236                 [WebCategory("Action")]
237                 public event EventHandler ServerClick {
238                         add { Events.AddHandler (serverClickEvent, value); }
239                         remove { Events.RemoveHandler (serverClickEvent, value); }
240                 }
241         }
242 }