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