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