[System.Web.Extensions] Update default profile and remove old profiles files
[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                                         HRef = ResolveClientUrl (hr);
168                         }
169
170                         base.RenderAttributes (writer);
171
172                         // but we never set back the href attribute after the rendering
173                         // nor is the property available after rendering
174                         Attributes.Remove ("href");
175                 }
176
177                 protected virtual void RaisePostBackEvent (string eventArgument)
178                 {
179                         ValidateEvent (UniqueID, eventArgument);
180                         if (CausesValidation)
181                                 Page.Validate (ValidationGroup);
182                         
183                         OnServerClick (EventArgs.Empty);
184                 }
185         
186                 PostBackOptions GetPostBackOptions ()
187                 {
188                         Page page = Page;
189                         PostBackOptions options = new PostBackOptions (this);
190                         options.ValidationGroup = null;
191                         options.ActionUrl = null;
192                         options.Argument = String.Empty;
193                         options.RequiresJavaScriptProtocol = true;
194                         options.ClientSubmit = true;
195                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
196                         if (options.PerformValidation)
197                                 options.ValidationGroup = ValidationGroup;
198
199                         return options;
200                 }
201
202                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
203                 {
204                         RaisePostBackEvent (eventArgument);
205                 }
206
207                 [WebSysDescription("")]
208                 [WebCategory("Action")]
209                 public event EventHandler ServerClick {
210                         add { Events.AddHandler (serverClickEvent, value); }
211                         remove { Events.RemoveHandler (serverClickEvent, value); }
212                 }
213         }
214 }