File: CheckBox.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (637 lines) | stat: -rw-r--r-- 22,520 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//------------------------------------------------------------------------------
// <copyright file="CheckBox.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Globalization;
    using System.Web;
    using System.Web.UI;
    using System.Web.Util;
    using AttributeCollection = System.Web.UI.AttributeCollection;


    /// <devdoc>
    ///    <para>Represents a Windows checkbox control.</para>
    /// </devdoc>
    [
    ControlValueProperty("Checked"),
    DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
    DefaultEvent("CheckedChanged"),
    Designer("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + AssemblyRef.SystemDesign),
    DefaultProperty("Text"),
    SupportsEventValidation,
    ]
    public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl {
        internal AttributeCollection _inputAttributes;
        private StateBag _inputAttributesState;
        private AttributeCollection _labelAttributes;
        private StateBag _labelAttributesState;
        private string _valueAttribute = null;

        private static readonly object EventCheckedChanged = new object();


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


        /// <devdoc>
        /// <para>Gets or sets a value indicating that the <see cref='System.Web.UI.WebControls.CheckBox'/> state is automatically posted back to
        ///    the
        ///    server.</para>
        /// </devdoc>
        [
        DefaultValue(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.CheckBox_AutoPostBack),
        Themeable(false),
        ]
        public virtual bool AutoPostBack {
            get {
                object b = ViewState["AutoPostBack"];
                return((b == null) ? false : (bool)b);
            }
            set {
                ViewState["AutoPostBack"] = value;
            }
        }


        [
        DefaultValue(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.AutoPostBackControl_CausesValidation),
        Themeable(false),
        ]
        public virtual bool CausesValidation {
            get {
                object b = ViewState["CausesValidation"];
                return((b == null) ? false : (bool)b);
            }
            set {
                ViewState["CausesValidation"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets a value indicating the checked state of the
        ///    <see cref='System.Web.UI.WebControls.CheckBox'/>.</para>
        /// </devdoc>
        [
        Bindable(true, BindingDirection.TwoWay),
        DefaultValue(false),
        Themeable(false),
        WebSysDescription(SR.CheckBox_Checked),
        ]
        public virtual bool Checked {
            get {
                object b = ViewState["Checked"];
                return((b == null) ? false : (bool)b);
            }
            set {
                ViewState["Checked"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Attribute collection for the rendered input element.</para>
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        WebSysDescription(SR.CheckBox_InputAttributes)
        ]
        public AttributeCollection InputAttributes {
            get {
                if (_inputAttributes == null) {

                    if (_inputAttributesState == null) {
                        _inputAttributesState = new StateBag(true);
                        if (IsTrackingViewState)
                            _inputAttributesState.TrackViewState();
                    }

                    _inputAttributes = new AttributeCollection(_inputAttributesState);
                }
                return _inputAttributes;
            }
        }


        /// <devdoc>
        ///    <para>Attribute collection for the rendered span element.</para>
        /// </devdoc>
        [
        Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        WebSysDescription(SR.CheckBox_LabelAttributes)
        ]
        public AttributeCollection LabelAttributes {
            get {
                if (_labelAttributes == null) {

                    if (_labelAttributesState == null) {
                        _labelAttributesState = new StateBag(true);
                        if (IsTrackingViewState)
                            _labelAttributesState.TrackViewState();
                    }

                    _labelAttributes = new AttributeCollection(_labelAttributesState);
                }
                return _labelAttributes;
            }
        }


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


        /// <devdoc>
        ///   Controls whether the Checked property is saved in ViewState.
        ///   This is used for optimizing the size of the view state.
        /// </devdoc>
        private bool SaveCheckedViewState(bool autoPostBack) {
            // Must be saved when
            // 1. There is a registered event handler for SelectedIndexChanged
            // 2. Control is not enabled or visible, because the browser's post data will not include this control
            // 3. The instance is a derived instance, which might be overriding the OnSelectedIndexChanged method
            //    This is a bit hacky, since we have to cover all the four derived classes we have...
            // 4. AutoPostBack is true and Adapter doesn't support JavaScript
            //    For CheckBoxes to behave the same on mobile devices
            //    that simulate AutoPostBack by rendering a command button, we need to save
            //    state
            //    

            if ((Events[EventCheckedChanged] != null) ||
                (IsEnabled == false) ||
                (Visible == false) ||
                (autoPostBack == true && ((Page != null) && !Page.ClientSupportsJavaScript))) {

                return true;
            }

            Type t = this.GetType();
            if ((t == typeof(CheckBox)) || (t == typeof(RadioButton))) {
                return false;
            }

            return true;
        }


        /// <devdoc>
        /// <para>Gets or sets the text label associated with the <see cref='System.Web.UI.WebControls.CheckBox'/>.</para>
        /// </devdoc>
        [
        Bindable(true),
        Localizable(true),
        WebCategory("Appearance"),
        DefaultValue(""),
        WebSysDescription(SR.CheckBox_Text)
        ]
        public virtual string Text {
            get {
                string s = (string)ViewState["Text"];
                return((s == null) ? String.Empty : s);
            }
            set {
                ViewState["Text"] = value;
            }
        }


        /// <devdoc>
        /// <para>Gets or sets the alignment of the <see langword='Text'/> associated with the <see cref='System.Web.UI.WebControls.CheckBox'/>.</para>
        /// </devdoc>
        [
        WebCategory("Appearance"),
        DefaultValue(TextAlign.Right),
        WebSysDescription(SR.WebControl_TextAlign)
        ]
        public virtual TextAlign TextAlign {
            get {
                object align = ViewState["TextAlign"];
                return((align == null) ? TextAlign.Right : (TextAlign)align);
            }
            set {
                if (value < TextAlign.Left || value > TextAlign.Right) {
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["TextAlign"] = value;
            }
        }


        [
        DefaultValue(""),
        Themeable(false),
        WebCategory("Behavior"),
        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 <see cref='System.Web.UI.WebControls.CheckBox'/> is clicked.</para>
        /// </devdoc>
        [
        WebCategory("Action"),
        WebSysDescription(SR.Control_OnServerCheckChanged)
        ]
        public event EventHandler CheckedChanged {
            add {
                Events.AddHandler(EventCheckedChanged, value);
            }
            remove {
                Events.RemoveHandler(EventCheckedChanged, value);
            }
        }


        /// <devdoc>
        ///     Adds attributes to be rendered.
        /// </devdoc>
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            // VSWhidbey 460446
            AddDisplayInlineBlockIfNeeded(writer);

            // Everett's behavior is that we didn't call the base method.
        }


        /// <devdoc>
        ///     Loads the view state for the control.
        /// </devdoc>
        protected override void LoadViewState(object savedState) {
            if (savedState != null) {
                Triplet stateTriplet = (Triplet)savedState;
                base.LoadViewState(stateTriplet.First);
                if (stateTriplet.Second != null) {
                    if (_inputAttributesState == null) {
                        _inputAttributesState = new StateBag();
                        _inputAttributesState.TrackViewState();
                    }
                    _inputAttributesState.LoadViewState(stateTriplet.Second);
                }
                if (stateTriplet.Third != null) {
                    if (_labelAttributesState == null) {
                        _labelAttributesState = new StateBag();
                        _labelAttributesState.TrackViewState();
                    }
                    _labelAttributesState.LoadViewState(stateTriplet.Second);
                }
            }
        }


        /// <devdoc>
        ///    <para> Raises the
        ///    <see langword='CheckedChanged'/> event of the <see cref='System.Web.UI.WebControls.CheckBox'/>
        ///    controls.</para>
        /// </devdoc>
        protected virtual void OnCheckedChanged(EventArgs e) {
            EventHandler handler = (EventHandler)Events[EventCheckedChanged];
            if (handler != null) {
                handler(this, e);
            }
        }


        /// <devdoc>
        ///    <para>Registers client script for generating postback prior to
        ///       rendering on the client if <see cref='System.Web.UI.WebControls.CheckBox.AutoPostBack'/> is
        ///    <see langword='true'/>.</para>
        /// </devdoc>
        protected internal override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            bool autoPostBack = AutoPostBack;

            if (Page != null && IsEnabled) {
                // we always need to get post back data
                Page.RegisterRequiresPostBack(this);
                if (autoPostBack) {
                    Page.RegisterPostBackScript();
                    Page.RegisterFocusScript();

                    // VSWhidbey 489577: It would handle both CheckBox and RadioButton cases
                    if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
                        Page.RegisterWebFormsScript();
                    }
                }
            }

            if (!SaveCheckedViewState(autoPostBack)) {
                ViewState.SetItemDirty("Checked", false);

                if ((Page != null) && IsEnabled) {
                    // Store a client-side array of enabled control, so we can re-enable them on
                    // postback (in case they are disabled client-side)
                    Page.RegisterEnabledControl(this);
                }
            }
        }


        /// <devdoc>
        ///     Saves the view state for the control.
        /// </devdoc>
        protected override object SaveViewState() {
            object baseState = base.SaveViewState();
            object inputState = null;
            object labelState = null;
            object myState = null;

            if (_inputAttributesState != null) {
                inputState = _inputAttributesState.SaveViewState();
            }
            if (_labelAttributesState != null) {
                labelState = _labelAttributesState.SaveViewState();
            }
            if (baseState != null || inputState != null || labelState != null) {
                myState = new Triplet(baseState, inputState, labelState);
            }
            return myState;
        }


        /// <devdoc>
        ///     Starts view state tracking.
        /// </devdoc>
        protected override void TrackViewState() {
            base.TrackViewState();
            if (_inputAttributesState != null) {
                _inputAttributesState.TrackViewState();
            }
            if (_labelAttributesState != null) {
                _labelAttributesState.TrackViewState();
            }
        }


        /// <internalonly/>
        /// <devdoc>
        /// <para>Displays the <see cref='System.Web.UI.WebControls.CheckBox'/> on the client.</para>
        /// </devdoc>
        protected internal override void Render(HtmlTextWriter writer) {
            AddAttributesToRender(writer);

            // Make sure we are in a form tag with runat=server.
            if (Page != null) {
                Page.VerifyRenderingInServerForm(this);
            }

            bool renderWrapper = false;

            // On wrapper, render ---- attribute and class according to RenderingCompatibility
            if (!IsEnabled) {
                if (RenderingCompatibility < VersionUtil.Framework40) {
                    writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    renderWrapper = true;
                }
                else if (!Enabled && !String.IsNullOrEmpty(DisabledCssClass)) {
                    if (String.IsNullOrEmpty(CssClass)) {
                        ControlStyle.CssClass = DisabledCssClass;
                    }
                    else {
                        ControlStyle.CssClass = DisabledCssClass + " " + CssClass;
                    }
                    renderWrapper = true;
                }
            }

            // And Style
            if (ControlStyleCreated) {
                Style controlStyle = ControlStyle;
                if (!controlStyle.IsEmpty) {
                    controlStyle.AddAttributesToRender(writer, this);
                    renderWrapper = true;
                }
            }
            // And ToolTip
            string toolTip = ToolTip;
            if (toolTip.Length > 0) {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, toolTip);
                renderWrapper = true;
            }

            string onClick = null;
            // And other attributes
            if (HasAttributes) {
                AttributeCollection attribs = Attributes;

                // remove value from the attribute collection so it's not on the wrapper
                string val = attribs["value"];
                if (val != null)
                    attribs.Remove("value");

                // remove and save onclick from the attribute collection so we can move it to the input tag
                onClick = attribs["onclick"];
                if (onClick != null) {
                    onClick = Util.EnsureEndWithSemiColon(onClick);
                    attribs.Remove("onclick");
                }

                if (attribs.Count != 0)
                {
                    attribs.AddAttributes(writer);
                    renderWrapper = true;
                }

                if (val != null)
                    attribs["value"] = val;
            }

            // render begin tag of wrapper SPAN
            if (renderWrapper) {
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
            }

            string text = Text;
            string clientID = ClientID;
            if (text.Length != 0) {
                if (TextAlign == TextAlign.Left) {
                    // render label to left of checkbox
                    RenderLabel(writer, text, clientID);
                    RenderInputTag(writer, clientID, onClick);
                }
                else {
                    // render label to right of checkbox
                    RenderInputTag(writer, clientID, onClick);
                    RenderLabel(writer, text, clientID);
                }
            }
            else
                RenderInputTag(writer, clientID, onClick);

            // render end tag of wrapper SPAN
            if (renderWrapper) {
                writer.RenderEndTag();
            }
        }

        private void RenderLabel(HtmlTextWriter writer, string text, string clientID) {
            writer.AddAttribute(HtmlTextWriterAttribute.For, clientID);

            if (_labelAttributes != null && _labelAttributes.Count != 0) {
                _labelAttributes.AddAttributes(writer);
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(text);
            writer.RenderEndTag();
        }

        internal virtual void RenderInputTag(HtmlTextWriter writer, string clientID, string onClick) {
            if (clientID != null) {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, clientID);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");

            if (UniqueID != null) {
                writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
            }

            // Whidbey 20815
            if (_valueAttribute != null) {
                writer.AddAttribute(HtmlTextWriterAttribute.Value, _valueAttribute);
            }

            if (Checked)
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");

            // ASURT 119141: Render ---- attribute on the INPUT tag (instead of the SPAN) so the checkbox actually gets disabled when Enabled=false
            if (!IsEnabled && SupportsDisabledAttribute) {
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }

            if (AutoPostBack && (Page != null) && Page.ClientSupportsJavaScript) {

                PostBackOptions options = new PostBackOptions(this, String.Empty);

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

                if (Page.Form != null) {
                    options.AutoPostBack = true;
                }

                // ASURT 98368
                // Need to merge the autopostback script with the user script
                onClick = Util.MergeScript(onClick, Page.ClientScript.GetPostBackEventReference(options, true));
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);

                if (EnableLegacyRendering) {
                    writer.AddAttribute("language", "javascript", false);
                }
            }
            else {
                if (Page != null) {
                    Page.ClientScript.RegisterForEventValidation(this.UniqueID);
                }

                if (onClick != null) {
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                }
            }

            string s = AccessKey;
            if (s.Length > 0)
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, s);

            int i = TabIndex;
            if (i != 0)
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, i.ToString(NumberFormatInfo.InvariantInfo));

            if (_inputAttributes != null && _inputAttributes.Count != 0) {
                _inputAttributes.AddAttributes(writer);
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }

        /// <internalonly/>
        /// <devdoc>
        /// <para>Processes posted data for the <see cref='System.Web.UI.WebControls.CheckBox'/>
        /// control.</para>
        /// </devdoc>
        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
            return LoadPostData(postDataKey, postCollection);
        }


        /// <internalonly/>
        /// <devdoc>
        /// <para>Processes posted data for the <see cref='System.Web.UI.WebControls.CheckBox'/>
        /// control.</para>
        /// </devdoc>
        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
            bool dataChanged = false;

            string post = postCollection[postDataKey];

            bool isChecked = (!String.IsNullOrEmpty(post));

            if (isChecked) {
                ValidateEvent(postDataKey);
            }

            dataChanged = (isChecked != Checked);
            Checked = isChecked;

            return dataChanged;
        }


        /// <internalonly/>
        /// <devdoc>
        /// Raises when posted data for a control has changed.
        /// </devdoc>
        void IPostBackDataHandler.RaisePostDataChangedEvent() {
            RaisePostDataChangedEvent();
        }


        /// <internalonly/>
        /// <devdoc>
        /// Raises when posted data for a control has changed.
        /// </devdoc>
        protected virtual void RaisePostDataChangedEvent() {
            if (AutoPostBack && !Page.IsPostBackEventControlRegistered) {
                // VSWhidbey 204824
                Page.AutoPostBackControl = this;

                if (CausesValidation) {
                    Page.Validate(ValidationGroup);
                }
            }
            OnCheckedChanged(EventArgs.Empty);
        }
    }
}