File: LinkButton.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (466 lines) | stat: -rw-r--r-- 16,420 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//------------------------------------------------------------------------------
// <copyright file="LinkButton.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {

    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Web;
    using System.Web.UI;
    using System.Web.Util;

    /// <devdoc>
    /// <para>Interacts with the parser to build a <see cref='System.Web.UI.WebControls.LinkButton'/> control.</para>
    /// </devdoc>
    public class LinkButtonControlBuilder : ControlBuilder {


        /// <internalonly/>
        /// <devdoc>
        ///    <para>Specifies whether white space literals are allowed.</para>
        /// </devdoc>
        public override bool AllowWhitespaceLiterals() {
            return false;
        }
    }



    /// <devdoc>
    ///    <para>Constructs a link button and defines its properties.</para>
    /// </devdoc>
    [
    ControlBuilderAttribute(typeof(LinkButtonControlBuilder)),
    DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
    DefaultEvent("Click"),
    DefaultProperty("Text"),
    ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>"),
    Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + AssemblyRef.SystemDesign),
    ParseChildren(false),
    SupportsEventValidation
    ]
    public class LinkButton : WebControl, IButtonControl, IPostBackEventHandler {

        private bool _textSetByAddParsedSubObject = false;
        private static readonly object EventClick = new object();
        private static readonly object EventCommand = new object();


        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.LinkButton'/> class.</para>
        /// </devdoc>
        public LinkButton() : base(HtmlTextWriterTag.A) {
        }


        /// <devdoc>
        ///    <para>Specifies the command name that is propagated in the
        ///    <see cref='System.Web.UI.WebControls.LinkButton.Command'/>event along with the associated <see cref='System.Web.UI.WebControls.LinkButton.CommandArgument'/>
        ///    property.</para>
        /// </devdoc>
        [
        DefaultValue(""),
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.WebControl_CommandName)
        ]
        public string CommandName {
            get {
                string s = (string)ViewState["CommandName"];
                return((s == null) ? String.Empty : s);
            }
            set {
                ViewState["CommandName"] = value;
            }
        }



        /// <devdoc>
        ///    <para>Specifies the command argument that is propagated in the
        ///    <see langword='Command '/>event along with the associated <see cref='System.Web.UI.WebControls.LinkButton.CommandName'/>
        ///    property.</para>
        /// </devdoc>
        [
        Bindable(true),
        DefaultValue(""),
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.WebControl_CommandArgument)
        ]
        public string CommandArgument {
            get {
                string s = (string)ViewState["CommandArgument"];
                return((s == null) ? String.Empty : s);
            }
            set {
                ViewState["CommandArgument"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets whether pressing the button causes page validation to fire. This defaults to True so that when
        ///          using validation controls, the validation state of all controls are updated when the button is clicked, both
        ///          on the client and the server. Setting this to False is useful when defining a cancel or reset button on a page
        ///          that has validators.</para>
        /// </devdoc>
        [
        DefaultValue(true),
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_CausesValidation)
        ]
        public virtual bool CausesValidation {
            get {
                object b = ViewState["CausesValidation"];
                return((b == null) ? true : (bool)b);
            }
            set {
                ViewState["CausesValidation"] = value;
            }
        }

        /// <devdoc>
        ///    The script that is executed on a client-side click.
        /// </devdoc>
        [
        DefaultValue(""),
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_OnClientClick)
        ]
        public virtual string OnClientClick {
            get {
                string s = (string)ViewState["OnClientClick"];
                if (s == null) {
                    return String.Empty;
                }
                return s;
            }
            set {
                ViewState["OnClientClick"] = value;
            }
        }

        public override bool SupportsDisabledAttribute {
            get {
                return RenderingCompatibility < VersionUtil.Framework40;
            }
        }

        internal override bool RequiresLegacyRendering {
            get {
                return true;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the text display for the link button.</para>
        /// </devdoc>
        [
        Localizable(true),
        Bindable(true),
        WebCategory("Appearance"),
        DefaultValue(""),
        WebSysDescription(SR.LinkButton_Text),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public virtual string Text {
            get {
                object o = ViewState["Text"];
                return((o == null) ? String.Empty : (string)o);
            }
            set {
                if (HasControls()) {
                    Controls.Clear();
                }
                ViewState["Text"] = value;
            }
        }


        [
        DefaultValue(""),
        Editor("System.Web.UI.Design.UrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        Themeable(false),
        UrlProperty("*.aspx"),
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_PostBackUrl)
        ]
        public virtual string PostBackUrl {
            get {
                string s = (string)ViewState["PostBackUrl"];
                return s == null? String.Empty : s;
            }
            set {
                ViewState["PostBackUrl"] = value;
            }
        }

        [
        WebCategory("Behavior"),
        Themeable(false),
        DefaultValue(""),
        WebSysDescription(SR.PostBackControl_ValidationGroup)
        ]
        public virtual string ValidationGroup {
            get {
                string s = (string)ViewState["ValidationGroup"];
                return((s == null) ? String.Empty : s);
            }
            set {
                ViewState["ValidationGroup"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Occurs when the link button is clicked.</para>
        /// </devdoc>
        [
        WebCategory("Action"),
        WebSysDescription(SR.LinkButton_OnClick)
        ]
        public event EventHandler Click {
            add {
                Events.AddHandler(EventClick, value);
            }
            remove {
                Events.RemoveHandler(EventClick, value);
            }
        }



        /// <devdoc>
        /// <para>Occurs when any item is clicked within the <see cref='System.Web.UI.WebControls.LinkButton'/> control tree.</para>
        /// </devdoc>
        [
        WebCategory("Action"),
        WebSysDescription(SR.Button_OnCommand)
        ]
        public event CommandEventHandler Command {
            add {
                Events.AddHandler(EventCommand, value);
            }
            remove {
                Events.RemoveHandler(EventCommand, value);
            }
        }


        /// <internalonly/>
        /// <devdoc>
        ///    Render the attributes on the begin tag.
        /// </devdoc>
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            // Make sure we are in a form tag with runat=server.
            if (Page != null) {
                Page.VerifyRenderingInServerForm(this);
            }

            // Need to merge the onclick attribute with the OnClientClick
            string onClick = Util.EnsureEndWithSemiColon(OnClientClick);

            if (HasAttributes) {
                string userOnClick = Attributes["onclick"];
                if (userOnClick != null) {
                    // We don't use Util.MergeScript because OnClientClick or
                    // onclick attribute are set by page developer directly.  We
                    // should preserve the value without adding javascript prefix.
                    onClick += Util.EnsureEndWithSemiColon(userOnClick);
                    Attributes.Remove("onclick");
                }
            }

            if (onClick.Length > 0) {
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
            }

            bool effectiveEnabled = IsEnabled;
            if (Enabled && !effectiveEnabled && SupportsDisabledAttribute) {
                // We need to do the cascade effect on the server, because the browser
                // only renders as disabled, but doesn't disable the functionality.
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }

            base.AddAttributesToRender(writer);

            if (effectiveEnabled && Page != null) {
                // 

                PostBackOptions options = GetPostBackOptions();
                string postBackEventReference = null;
                if (options != null) {
                    postBackEventReference = Page.ClientScript.GetPostBackEventReference(options, true);
                }

                // If the postBackEventReference is empty, use a javascript no-op instead, since
                // <a href="" /> is a link to the root of the current directory.
                if (String.IsNullOrEmpty(postBackEventReference)) {
                    postBackEventReference = "javascript:void(0)";
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
            }
        }


        /// <internalonly/>
        /// <devdoc>
        /// </devdoc>
        protected override void AddParsedSubObject(object obj) {
            if (HasControls()) {
                base.AddParsedSubObject(obj);
            }
            else {
                if (obj is LiteralControl) {
                    if (_textSetByAddParsedSubObject) {
                        Text += ((LiteralControl)obj).Text;
                    }
                    else {
                        Text = ((LiteralControl)obj).Text;
                    }
                    _textSetByAddParsedSubObject = true;
                }
                else {
                    string currentText = Text;
                    if (currentText.Length != 0) {
                        Text = String.Empty;
                        base.AddParsedSubObject(new LiteralControl(currentText));
                    }
                    base.AddParsedSubObject(obj);
                }
            }
        }

        // Returns the client post back options.
        protected virtual PostBackOptions GetPostBackOptions() {
            PostBackOptions options = new PostBackOptions(this, String.Empty);
            options.RequiresJavaScriptProtocol = true;

            if (!String.IsNullOrEmpty(PostBackUrl)) {
                // VSWhidbey 424614: Since the url is embedded as javascript in attribute,
                // we should match the same encoding as done on HyperLink.NavigateUrl value.
                options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl));

                // Also, there is a specific behavior in IE that when the script
                // is triggered in href attribute, the whole string will be
                // decoded once before the code is run.  This doesn't happen to
                // onclick or other event attributes.  So here we do an extra
                // encoding to compensate the weird behavior on IE.
                if (!DesignMode && Page != null &&
                    String.Equals(Page.Request.Browser.Browser, "IE", StringComparison.OrdinalIgnoreCase)) {
                    options.ActionUrl = Util.QuoteJScriptString(options.ActionUrl, true);
                }
            }

            if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
                options.PerformValidation = true;
                options.ValidationGroup = ValidationGroup;
            }

            return options;
        }


        /// <internalonly/>
        /// <devdoc>
        ///    Load previously saved state.
        ///    Overridden to synchronize Text property with LiteralContent.
        /// </devdoc>
        protected override void LoadViewState(object savedState) {
            if (savedState != null) {
                base.LoadViewState(savedState);
                string s = (string)ViewState["Text"];
                // Dev10 703061 If Text is set, we want to clear out any child controls, but not dirty viewstate
                if (s != null && HasControls()) {
                    Controls.Clear();
                }
            }
        }


        /// <devdoc>
        /// <para>Raises the <see langword='Click '/> event.</para>
        /// </devdoc>
        protected virtual void OnClick(EventArgs e) {
            EventHandler onClickHandler = (EventHandler)Events[EventClick];
            if (onClickHandler != null) onClickHandler(this,e);
        }


        /// <devdoc>
        /// <para>Raises the <see langword='Command'/> event.</para>
        /// </devdoc>
        protected virtual void OnCommand(CommandEventArgs e) {
            CommandEventHandler onCommandHandler = (CommandEventHandler)Events[EventCommand];
            if (onCommandHandler != null)
                onCommandHandler(this,e);

            // Command events are bubbled up the control heirarchy
            RaiseBubbleEvent(this, e);
        }


        /// <internalonly/>
        /// <devdoc>
        /// <para>Raises a <see langword='Click '/>event upon postback
        /// to the server, and a <see langword='Command'/> event if the <see cref='System.Web.UI.WebControls.LinkButton.CommandName'/>
        /// is defined.</para>
        /// </devdoc>
        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
            RaisePostBackEvent(eventArgument);
        }


        /// <internalonly/>
        /// <devdoc>
        /// <para>Raises a <see langword='Click '/>event upon postback
        /// to the server, and a <see langword='Command'/> event if the <see cref='System.Web.UI.WebControls.LinkButton.CommandName'/>
        /// is defined.</para>
        /// </devdoc>
        protected virtual void RaisePostBackEvent(string eventArgument) {
            ValidateEvent(this.UniqueID, eventArgument);
            if (CausesValidation) {
                Page.Validate(ValidationGroup);
            }
            OnClick(EventArgs.Empty);
            OnCommand(new CommandEventArgs(CommandName, CommandArgument));
        }


        /// <internalonly/>
        protected internal override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            if (Page != null && Enabled) {
                Page.RegisterPostBackScript();

                if ((CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) ||
                     !String.IsNullOrEmpty(PostBackUrl)) {
                    Page.RegisterWebFormsScript();  // VSWhidbey 489577
                }
            }
        }


        /// <internalonly/>
        /// <devdoc>
        /// </devdoc>
        protected internal override void RenderContents(HtmlTextWriter writer) {
            if (HasRenderingData()) {
                base.RenderContents(writer);
            }
            else {
                writer.Write(Text);
            }
        }
    }
}