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 private static readonly object serverClickEvent = new object ();
53 [WebSysDescription("")]
54 [WebCategory("Action")]
55 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
61 string s = Attributes ["href"];
62 return (s == null) ? String.Empty : s;
66 Attributes.Remove ("href");
68 Attributes ["href"] = value;
74 [WebSysDescription("")]
75 [WebCategory("Navigation")]
76 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79 string s = Attributes ["name"];
80 return (s == null) ? String.Empty : s;
84 Attributes.Remove ("name");
86 Attributes ["name"] = value;
91 [WebSysDescription("")]
92 [WebCategory("Navigation")]
93 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
94 public string Target {
96 string s = Attributes ["target"];
97 return (s == null) ? String.Empty : s;
101 Attributes.Remove ("target");
103 Attributes ["target"] = value;
108 [WebSysDescription("")]
109 [WebCategory("Appearance")]
110 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114 public string Title {
116 string s = Attributes ["title"];
117 return (s == null) ? String.Empty : s;
121 Attributes.Remove ("title");
123 Attributes ["title"] = value;
128 [DefaultValue (true)]
129 public virtual bool CausesValidation {
131 return ViewState.GetBool ("CausesValidation", true);
134 ViewState ["CausesValidation"] = value;
139 public virtual string ValidationGroup {
141 return ViewState.GetString ("ValidationGroup", String.Empty);
144 ViewState ["ValidationGroup"] = value;
154 override void OnPreRender (EventArgs e)
156 base.OnPreRender (e);
159 protected virtual void OnServerClick (EventArgs e)
161 EventHandler serverClick = (EventHandler) Events [serverClickEvent];
162 if (serverClick != null)
163 serverClick (this, e);
166 protected override void RenderAttributes (HtmlTextWriter writer)
168 // we don't want to render the "user" URL, so we either render:
170 EventHandler serverClick = (EventHandler) Events [serverClickEvent];
171 if (serverClick != null) {
174 PostBackOptions options = GetPostBackOptions ();
175 Attributes ["href"] = Page.ClientScript.GetPostBackEventReference (options);
178 ClientScriptManager csm = new ClientScriptManager (Page);
179 Attributes ["href"] = csm.GetPostBackClientHyperlink (this, String.Empty);
184 HRef = ResolveUrl (hr);
187 string target = Attributes ["target"];
188 if ((target == null) || (target.Length == 0))
189 Attributes.Remove("target");
191 base.RenderAttributes (writer);
193 // but we never set back the href attribute after the rendering
194 // nor is the property available after rendering
195 Attributes.Remove ("href");
198 protected virtual void RaisePostBackEvent (string eventArgument)
200 if (CausesValidation)
201 Page.Validate (ValidationGroup);
203 OnServerClick (EventArgs.Empty);
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;
221 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
224 RaisePostBackEvent (eventArgument);
226 OnServerClick (EventArgs.Empty);
231 [WebSysDescription("")]
232 [WebCategory("Action")]
233 public event EventHandler ServerClick {
234 add { Events.AddHandler (serverClickEvent, value); }
235 remove { Events.RemoveHandler (serverClickEvent, value); }