2 // System.Web.UI.HtmlControls.HtmlAnchor.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.HtmlControls {
35 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [DefaultEvent ("ServerClick")]
40 [SupportsEventValidation]
42 public class HtmlAnchor : HtmlContainerControl, IPostBackEventHandler
44 static readonly object serverClickEvent = new object ();
52 [WebSysDescription("")]
53 [WebCategory("Action")]
54 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
60 string s = Attributes ["href"];
61 return (s == null) ? String.Empty : s;
64 if (value == null || value.Length == 0) {
65 Attributes.Remove ("href");
67 Attributes ["href"] = value;
73 [WebSysDescription("")]
74 [WebCategory("Navigation")]
75 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
78 string s = Attributes ["name"];
79 return (s == null) ? String.Empty : s;
82 if (value == null || value.Length == 0)
83 Attributes.Remove ("name");
85 Attributes ["name"] = value;
90 [WebSysDescription("")]
91 [WebCategory("Navigation")]
92 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93 public string Target {
95 string s = Attributes ["target"];
96 return (s == null) ? String.Empty : s;
99 if (value == null || value.Length == 0)
100 Attributes.Remove ("target");
102 Attributes ["target"] = value;
107 [WebSysDescription("")]
108 [WebCategory("Appearance")]
109 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113 public string Title {
115 string s = Attributes ["title"];
116 return (s == null) ? String.Empty : s;
119 if (value == null || value.Length == 0)
120 Attributes.Remove ("title");
122 Attributes ["title"] = value;
127 [DefaultValue (true)]
128 public virtual bool CausesValidation {
130 return ViewState.GetBool ("CausesValidation", true);
133 ViewState ["CausesValidation"] = value;
138 public virtual string ValidationGroup {
140 return ViewState.GetString ("ValidationGroup", String.Empty);
143 ViewState ["ValidationGroup"] = value;
153 override void OnPreRender (EventArgs e)
155 base.OnPreRender (e);
158 protected virtual void OnServerClick (EventArgs e)
160 EventHandler serverClick = (EventHandler) Events [serverClickEvent];
161 if (serverClick != null)
162 serverClick (this, e);
165 protected override void RenderAttributes (HtmlTextWriter writer)
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;
173 PostBackOptions options = GetPostBackOptions ();
174 csm = Page.ClientScript;
175 csm.RegisterForEventValidation (options);
176 Attributes ["href"] = csm.GetPostBackEventReference (options, true);
179 csm = new ClientScriptManager (Page);
180 Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
184 if (hr != string.Empty)
186 // For J2EE portlets we need to genreate a render URL.
187 HRef = ResolveClientUrl (hr, String.Compare (Target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
189 HRef = ResolveClientUrl (hr);
193 base.RenderAttributes (writer);
195 // but we never set back the href attribute after the rendering
196 // nor is the property available after rendering
197 Attributes.Remove ("href");
201 protected virtual void RaisePostBackEvent (string eventArgument)
203 ValidateEvent (UniqueID, eventArgument);
204 if (CausesValidation)
205 Page.Validate (ValidationGroup);
207 OnServerClick (EventArgs.Empty);
210 PostBackOptions GetPostBackOptions ()
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;
226 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
229 RaisePostBackEvent (eventArgument);
231 OnServerClick (EventArgs.Empty);
235 [WebSysDescription("")]
236 [WebCategory("Action")]
237 public event EventHandler ServerClick {
238 add { Events.AddHandler (serverClickEvent, value); }
239 remove { Events.RemoveHandler (serverClickEvent, value); }