1 //------------------------------------------------------------------------------
2 // <copyright file="HtmlAnchor.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 /*
8  * HtmlAnchor.cs
9  *
10  * Copyright (c) 2000 Microsoft Corporation
11  */
12 
13 namespace System.Web.UI.HtmlControls {
14     using System.ComponentModel;
15     using System;
16     using System.Collections;
17     using System.Web;
18     using System.Web.UI;
19     using System.Web.Util;
20     using System.Security.Permissions;
21 
22 
23     /// <devdoc>
24     /// <para>The <see langword='HtmlAnchor'/>
25     /// class defines the methods, properties, and
26     /// events for the HtmlAnchor control.
27     /// This
28     /// class
29     /// allows programmatic access to the
30     /// HTML &lt;a&gt; element on the server.</para>
31     /// </devdoc>
32     [
33     DefaultEvent("ServerClick"),
34     SupportsEventValidation,
35     ]
36     public class HtmlAnchor : HtmlContainerControl, IPostBackEventHandler {
37 
38         private static readonly object EventServerClick = new object();
39 
40         /*
41          *  Creates an intrinsic Html A control.
42          */
43 
44         /// <devdoc>
45         /// <para>Initializes a new instance of the <see cref='System.Web.UI.HtmlControls.HtmlAnchor'/> class.</para>
46         /// </devdoc>
HtmlAnchor()47         public HtmlAnchor() : base("a") {
48         }
49 
50         [
51         WebCategory("Behavior"),
52         DefaultValue(true),
53         ]
54         public virtual bool CausesValidation {
55             get {
56                 object b = ViewState["CausesValidation"];
57                 return((b == null) ? true : (bool)b);
58             }
59             set {
60                 ViewState["CausesValidation"] = value;
61             }
62         }
63 
64         /*
65          * Href property.
66          */
67 
68         /// <devdoc>
69         ///    <para>Gets or sets the URL target of the link specified in the
70         ///    <see cref='System.Web.UI.HtmlControls.HtmlAnchor'/>
71         ///    server control.</para>
72         /// </devdoc>
73         [
74         WebCategory("Navigation"),
75         DefaultValue(""),
76         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
77         UrlProperty()
78         ]
79         public string HRef {
80             get {
81                 string s = Attributes["href"];
82                 return((s != null) ? s : String.Empty);
83             }
84             set {
85                 Attributes["href"] = MapStringAttributeToString(value);
86             }
87         }
88 
89         /*
90          * Name of group this radio is in.
91          */
92 
93         /// <devdoc>
94         /// <para>Gets or sets the bookmark name defined in the <see cref='System.Web.UI.HtmlControls.HtmlAnchor'/>
95         /// server
96         /// control.</para>
97         /// </devdoc>
98         [
99         WebCategory("Navigation"),
100         DefaultValue(""),
101         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
102         ]
103         public string Name {
104             get {
105                 string s = Attributes["name"];
106                 return((s != null) ? s : String.Empty);
107             }
108             set {
109                 Attributes["name"] = MapStringAttributeToString(value);
110             }
111         }
112 
113         /*
114          * Target window property.
115          */
116 
117         /// <devdoc>
118         ///    <para>
119         ///       Gets or
120         ///       sets the target window or frame
121         ///       to load linked Web page content into.
122         ///    </para>
123         /// </devdoc>
124         [
125         WebCategory("Navigation"),
126         DefaultValue(""),
127         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
128         ]
129         public string Target {
130             get {
131                 string s = Attributes["target"];
132                 return((s != null) ? s : String.Empty);
133             }
134             set {
135                 Attributes["target"] = MapStringAttributeToString(value);
136             }
137         }
138 
139         /*
140          * Title property.
141          */
142 
143         /// <devdoc>
144         ///    <para> Gets or sets the title that
145         ///       the browser displays when identifying linked content.</para>
146         /// </devdoc>
147         [
148         WebCategory("Appearance"),
149         Localizable(true),
150         DefaultValue(""),
151         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
152         ]
153         public string Title {
154             get {
155                 string s = Attributes["title"];
156                 return((s != null) ? s : String.Empty);
157             }
158             set {
159                 Attributes["title"] = MapStringAttributeToString(value);
160             }
161         }
162 
163         [
164         WebCategory("Behavior"),
165         DefaultValue(""),
166         WebSysDescription(SR.PostBackControl_ValidationGroup)
167         ]
168         public virtual string ValidationGroup {
169             get {
170                 string s = (string)ViewState["ValidationGroup"];
171                 return((s == null) ? string.Empty : s);
172             }
173             set {
174                 ViewState["ValidationGroup"] = value;
175             }
176         }
177 
178         /// <devdoc>
179         /// <para>Occurs on the server when a user clicks the <see cref='System.Web.UI.HtmlControls.HtmlAnchor'/> control on the
180         ///    browser.</para>
181         /// </devdoc>
182         [
183         WebCategory("Action"),
184         WebSysDescription(SR.HtmlControl_OnServerClick)
185         ]
186         public event EventHandler ServerClick {
187             add {
188                 Events.AddHandler(EventServerClick, value);
189             }
190             remove {
191                 Events.RemoveHandler(EventServerClick, value);
192             }
193         }
194 
GetPostBackOptions()195         private PostBackOptions GetPostBackOptions() {
196             PostBackOptions options = new PostBackOptions(this, string.Empty);
197             options.RequiresJavaScriptProtocol = true;
198 
199             if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
200                 options.PerformValidation = true;
201                 options.ValidationGroup = ValidationGroup;
202             }
203 
204             return options;
205         }
206 
207         /// <internalonly/>
OnPreRender(EventArgs e)208         protected internal override void OnPreRender(EventArgs e) {
209             base.OnPreRender(e);
210             if (Page != null && Events[EventServerClick] != null) {
211                 Page.RegisterPostBackScript();
212 
213                 // VSWhidbey 489577
214                 if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
215                     Page.RegisterWebFormsScript();
216                 }
217             }
218         }
219 
220         /*
221          * Override to generate postback code for onclick.
222          */
223 
224         /// <internalonly/>
225         /// <devdoc>
226         /// </devdoc>
RenderAttributes(HtmlTextWriter writer)227         protected override void RenderAttributes(HtmlTextWriter writer) {
228             if (Events[EventServerClick] != null) {
229                 Attributes.Remove("href");
230                 base.RenderAttributes(writer);
231 
232                 PostBackOptions options = GetPostBackOptions();
233                 Debug.Assert(options != null);
234                 string postBackEventReference = Page.ClientScript.GetPostBackEventReference(options, true);
235 
236                 Debug.Assert(!string.IsNullOrEmpty(postBackEventReference));
237                 writer.WriteAttribute("href", postBackEventReference, true);
238             }
239             else {
240                 PreProcessRelativeReferenceAttribute(writer, "href");
241                 base.RenderAttributes(writer);
242             }
243         }
244 
245         /*
246          * Method used to raise the OnServerClick event.
247          */
248 
249         /// <devdoc>
250         /// <para>Raises the <see langword='ServerClick'/>
251         /// event.</para>
252         /// </devdoc>
OnServerClick(EventArgs e)253         protected virtual void OnServerClick(EventArgs e) {
254             EventHandler handler = (EventHandler)Events[EventServerClick];
255             if (handler != null) handler(this, e);
256         }
257 
258         /*
259          * Method of IPostBackEventHandler interface to raise events on post back.
260          * Button fires an OnServerClick event.
261          */
262 
263         /// <internalonly/>
264         /// <devdoc>
265         /// </devdoc>
IPostBackEventHandler.RaisePostBackEvent(string eventArgument)266         void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
267             RaisePostBackEvent(eventArgument);
268         }
269 
270 
271         /// <internalonly/>
272         /// <devdoc>
273         /// </devdoc>
RaisePostBackEvent(string eventArgument)274         protected virtual void RaisePostBackEvent(string eventArgument) {
275             ValidateEvent(UniqueID, eventArgument);
276 
277             if (CausesValidation) {
278                 Page.Validate(ValidationGroup);
279             }
280             OnServerClick(EventArgs.Empty);
281         }
282     }
283 }
284