File: BoundField.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 (654 lines) | stat: -rw-r--r-- 23,764 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//------------------------------------------------------------------------------
// <copyright file="BoundField.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.Util;


    /// <devdoc>
    /// <para>Creates a field bounded to a data field in a <see cref='System.Web.UI.WebControls.DataBoundControl'/>.</para>
    /// </devdoc>
    public class BoundField : DataControlField {


        /// <devdoc>
        ///    <para>Specifies a string that represents "this". This field is read-only. </para>
        /// </devdoc>
        public static readonly string ThisExpression = "!";
        private static readonly string _expressionPartSeparator = ".";

        private PropertyDescriptor _boundFieldDesc;
        private bool _boundFieldDescInitialized;
        string _dataField;
        string _dataFormatString;
        bool _htmlEncode;
        bool _htmlEncodeSet = false;
        bool _suppressHeaderTextFieldChange;
        private bool _htmlEncodeFormatString;
        private bool _htmlEncodeFormatStringSet;


        /// <devdoc>
        /// <para>Initializes a new instance of a <see cref='System.Web.UI.WebControls.BoundField'/> class.</para>
        /// </devdoc>
        public BoundField() {
        }

        /// <summary>
        /// Determines whether the control validates client input or not, defaults to inherit from parent.
        /// </summary>
        [
        WebCategory("Behavior"),
        WebSysDescription(SR.Control_ValidateRequestMode),
        DefaultValue(ValidateRequestMode.Inherit)
        ]
        public new ValidateRequestMode ValidateRequestMode {
            get {
                return base.ValidateRequestMode;
            }
            set {
                base.ValidateRequestMode = value;
            }
        }

        /// <devdoc>
        ///    <para> Indicates whether to apply the DataFormatString in edit mode</para>
        /// </devdoc>
        [
            WebCategory("Behavior"),
            DefaultValue(false),
            WebSysDescription(SR.BoundField_ApplyFormatInEditMode)
        ]
        public virtual bool ApplyFormatInEditMode {
            get {
                object o = ViewState["ApplyFormatInEditMode"];
                if (o != null) {
                    return (bool)o;
                }
                return false;
            }
            set {
                ViewState["ApplyFormatInEditMode"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the property that determines whether the BoundField treats empty string as
        ///    null when the field values are extracted.</para>
        /// </devdoc>
        [
            WebCategory("Behavior"),
            DefaultValue(true),
            WebSysDescription(SR.BoundField_ConvertEmptyStringToNull)
        ]
        public virtual bool ConvertEmptyStringToNull {
            get {
                object o = ViewState["ConvertEmptyStringToNull"];
                if (o != null) {
                    return (bool)o;
                }
                return true;
            }
            set {
                ViewState["ConvertEmptyStringToNull"] = value;
            }
        }
        

        /// <devdoc>
        ///    <para> Gets or sets the field name from the data model bound to this field.</para>
        /// </devdoc>
        [
            WebCategory("Data"),
            DefaultValue(""),
            TypeConverter("System.Web.UI.Design.DataSourceViewSchemaConverter, " + AssemblyRef.SystemDesign),
            WebSysDescription(SR.BoundField_DataField)
        ]
        public virtual string DataField {
            get {
                if (_dataField == null) {
                    object o = ViewState["DataField"];
                    if (o != null)
                        _dataField = (string)o;
                    else
                        _dataField = String.Empty;
                    }
                return _dataField;
            }
            set {
                if (!String.Equals(value, ViewState["DataField"])) {
                    ViewState["DataField"] = value;
                    _dataField = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the display format of data in this
        ///       field.</para>
        /// </devdoc>
        [
            WebCategory("Data"),
            DefaultValue(""),
            WebSysDescription(SR.BoundField_DataFormatString)
        ]
        public virtual string DataFormatString {
            get {
                if (_dataFormatString == null) {
                    object o = ViewState["DataFormatString"];
                    if (o != null)
                        _dataFormatString = (string)o;
                    else 
                        _dataFormatString = String.Empty;
                }
                return _dataFormatString;
            }
            set {
                if (!String.Equals(value, ViewState["DataFormatString"])) {
                    ViewState["DataFormatString"] = value;
                    _dataFormatString = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        /// <para>Gets or sets the text displayed in the header of the
        /// System.Web.UI.WebControls.Field.</para>
        /// </devdoc>
        public override string HeaderText {
            get {
                return base.HeaderText;
            }
            set {
                if (!String.Equals(value, ViewState["HeaderText"])) {
                    ViewState["HeaderText"] = value;
                    if (!_suppressHeaderTextFieldChange) {
                        OnFieldChanged();
                    }
                }
            }
        }

        /// <devdoc>
        /// <para>Gets or sets a property indicating whether data should be HtmlEncoded when it is displayed to the user.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(true),
        WebSysDescription(SR.BoundField_HtmlEncode)
        ]
        public virtual bool HtmlEncode {
            get {
                if (!_htmlEncodeSet) {
                    object o = ViewState["HtmlEncode"];
                    if (o != null) {
                        _htmlEncode = (bool)o;
                    }
                    else {
                        _htmlEncode = true;
                    }
                    _htmlEncodeSet = true;
                }
                return _htmlEncode;
            }
            set {
                object oldValue = ViewState["HtmlEncode"];
                if (oldValue == null || (bool)oldValue != value) {
                    ViewState["HtmlEncode"] = value;
                    _htmlEncode = value;
                    _htmlEncodeSet = true;
                    OnFieldChanged();
                }
            }
        }

        /// <devdoc>
        /// <para>Gets or sets a property indicating whether the format string should be HtmlEncoded
        /// when it is displayed to the user.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(true),
        ]
        public virtual bool HtmlEncodeFormatString {
            get {
                if (!_htmlEncodeFormatStringSet) {
                    object o = ViewState["HtmlEncodeFormatString"];
                    if (o != null) {
                        _htmlEncodeFormatString = (bool)o;
                    }
                    else {
                        _htmlEncodeFormatString = true;
                    }
                    _htmlEncodeFormatStringSet = true;
                }
                return _htmlEncodeFormatString;
            }
            set {
                object oldValue = ViewState["HtmlEncodeFormatString"];
                if (oldValue == null || (bool)oldValue != value) {
                    ViewState["HtmlEncodeFormatString"] = value;
                    _htmlEncodeFormatString = value;
                    _htmlEncodeFormatStringSet = true;
                    OnFieldChanged();
                }
            }
        }
        /// <devdoc>
        ///    <para>Gets or sets the property that determines what text is displayed if the value
        ///    of the field is null.</para>
        /// </devdoc>
        [
            WebCategory("Behavior"),
            DefaultValue(""),
            WebSysDescription(SR.BoundField_NullDisplayText)
        ]
        public virtual string NullDisplayText {
            get {
                object o = ViewState["NullDisplayText"];
                if (o != null) {
                    return (string)o;
                }
                return String.Empty;
            }
            set {
                if (!String.Equals(value, ViewState["NullDisplayText"])) {
                    ViewState["NullDisplayText"] = value;
                    OnFieldChanged();
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the property that prevents modification to data
        ///       in this field.</para>
        /// </devdoc>
        [
            WebCategory("Behavior"),
            DefaultValue(false),
            WebSysDescription(SR.BoundField_ReadOnly)
        ]
        public virtual bool ReadOnly {
            get {
                object o = ViewState["ReadOnly"];
                if (o != null)
                    return (bool)o;
                return false;
            }
            set {
                object oldValue = ViewState["ReadOnly"];
                if (oldValue == null || (bool)oldValue != value) {
                    ViewState["ReadOnly"] = value;
                    OnFieldChanged();
                }
            }
        }

        protected virtual bool SupportsHtmlEncode {
            get {
                return true;
            }
        }

        protected override void CopyProperties(DataControlField newField) {
            ((BoundField)newField).ApplyFormatInEditMode = ApplyFormatInEditMode;
            ((BoundField)newField).ConvertEmptyStringToNull = ConvertEmptyStringToNull;
            ((BoundField)newField).DataField = DataField;
            ((BoundField)newField).DataFormatString = DataFormatString;
            ((BoundField)newField).HtmlEncode = HtmlEncode;
            ((BoundField)newField).HtmlEncodeFormatString = HtmlEncodeFormatString;
            ((BoundField)newField).NullDisplayText = NullDisplayText;
            ((BoundField)newField).ReadOnly = ReadOnly;

            base.CopyProperties(newField);
        }

        protected override DataControlField CreateField() {
            return new BoundField();
        }
        

        /// <devdoc>
        /// Extracts the value(s) from the given cell and puts the value(s) into a dictionary.  Indicate includeReadOnly
        /// to have readonly fields' values inserted into the dictionary.
        /// </devdoc>
        public override void ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) {
            Control childControl = null;
            string dataField = DataField;
            object value = null;
            string nullDisplayText = NullDisplayText;

            if (((rowState & DataControlRowState.Insert) != 0) && !InsertVisible) {
                return;
            }

            if (cell.Controls.Count > 0) {
                childControl = cell.Controls[0];

                TextBox editBox = childControl as TextBox;
                if (editBox != null) {
                    value = editBox.Text;
                }
            }
            else {
                if (includeReadOnly == true) {
                    string cellText = cell.Text;
                    if (cellText == "&nbsp;") { // nothing HtmlEncodes to &nbsp;, so we know that this means it was empty.
                        value = String.Empty;
                    }
                    else {
                        if (SupportsHtmlEncode && HtmlEncode) {
                            value = HttpUtility.HtmlDecode(cellText);
                        }
                        else {
                            value = cellText;
                        }
                    }
                }
            }

            if (value != null) {
                if ((value is string) && (((string)value).Length == 0) && ConvertEmptyStringToNull) {
                    value = null;
                }

                if (value is string && (string)value == nullDisplayText && nullDisplayText.Length > 0) {
                    value = null;
                }

                if (dictionary.Contains(dataField)) {
                    dictionary[dataField] = value;
                }
                else {
                    dictionary.Add(dataField, value);
                }
            }

        }


        /// <devdoc>
        /// Returns the value of the field formatted as text for the cell.
        /// </devdoc>
        protected virtual string FormatDataValue(object dataValue, bool encode) {
            string formattedValue = String.Empty;

            if (!DataBinder.IsNull(dataValue)) {
                string dataValueString = dataValue.ToString();
                string formatting = DataFormatString;
                int dataValueStringLength = dataValueString.Length;

                if (!HtmlEncodeFormatString) {
                    // Back-compat (Whidbey) behavior when HtmlEncodeFormatString=false
                    if (dataValueStringLength > 0 && encode) {
                        dataValueString = HttpUtility.HtmlEncode(dataValueString);
                    }

                    if (dataValueStringLength == 0 && ConvertEmptyStringToNull) {
                        formattedValue = NullDisplayText;
                    }
                    else if (formatting.Length == 0) {
                        formattedValue = dataValueString;
                    }
                    else {
                        if (encode) {
                            formattedValue = String.Format(CultureInfo.CurrentCulture, formatting, dataValueString);
                        }
                        else {
                            formattedValue = String.Format(CultureInfo.CurrentCulture, formatting, dataValue);
                        }
                    }
                }
                else {
                    // New default behavior (Orcas) when HtmlEncodeFormatString=true

                    // If the result is still empty and ConvertEmptyStringToNull=true, replace the value with the NullDisplayText
                    if (dataValueStringLength == 0 && ConvertEmptyStringToNull) {
                        dataValueString = NullDisplayText;
                    }
                    else {
                        // If there's a format string, apply it to the raw data value
                        // If there's no format string, then dataValueString already has the right value
                        if (!String.IsNullOrEmpty(formatting)) {
                            dataValueString = String.Format(CultureInfo.CurrentCulture, formatting, dataValue);
                        }

                        // Optionally HTML encode the value (including the format string, if any was applied)
                        if (!String.IsNullOrEmpty(dataValueString) && encode) {
                            dataValueString = HttpUtility.HtmlEncode(dataValueString);
                        }
                    }

                    formattedValue = dataValueString;
                }
            }
            else {
                formattedValue = NullDisplayText;
            }

            return formattedValue;
        }


        /// <devdoc>
        /// Returns a value to be used for design-time rendering
        /// </devdoc>
        protected virtual object GetDesignTimeValue() {
            return SR.GetString(SR.Sample_Databound_Text);
        }


        /// <devdoc>
        /// Retrieves the value of the field to be databound to the BoundField.
        /// </devdoc>
        protected virtual object GetValue(Control controlContainer) {
            Debug.Assert(DataField.Length != 0, "Shouldn't be DataBinding without a DataField");

            object data = null;
            object dataItem = null;
            string boundField = DataField;

            if (controlContainer == null) {
                throw new HttpException(SR.GetString(SR.DataControlField_NoContainer));
            }

            dataItem = DataBinder.GetDataItem(controlContainer);

            if (dataItem == null) {
                if (DesignMode) {
                    return GetDesignTimeValue();
                }
                else {
                    throw new HttpException(SR.GetString(SR.DataItem_Not_Found));
                }
            }

            if (boundField.Equals(ThisExpression)) {
                if (DesignMode) {
                    return GetDesignTimeValue();
                }
                else {
                    return dataItem;
                }
            }

            if (!TryGetSimplePropertyValue(dataItem, out data)) {
                data = DataBinder.Eval(dataItem, boundField);
            }

            return data;
        }

        private bool TryGetSimplePropertyValue(object dataItem, out object data) {
            string boundField = DataField;
            data = null;

            if (!_boundFieldDescInitialized) {
                //For simple properties , we cache the property descriptor for performance reasons.
                _boundFieldDesc = TypeDescriptor.GetProperties(dataItem).Find(boundField, true);
                _boundFieldDescInitialized = true;
            }

            if (_boundFieldDesc != null) {
                // simple property case
                data = _boundFieldDesc.GetValue(dataItem);
                return true;
            }
            else if (DesignMode) {
                data = GetDesignTimeValue();
                return true;
            }
            else if (!boundField.Contains(_expressionPartSeparator)) {
                throw new HttpException(SR.GetString(SR.Field_Not_Found, boundField));
            }
            else {
                // complex property case
                return false;
            }
        }

        /// <devdoc>
        /// Initializes the field and resets member variables.
        /// </devdoc>
        public override bool Initialize(bool enableSorting, Control control) {
            base.Initialize(enableSorting, control);
            _boundFieldDesc = null;
            _boundFieldDescInitialized = false;
            return false;
        }


        /// <devdoc>
        /// <para>Initializes a cell in the DataControlField.</para>
        /// </devdoc>
        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) {
            string unencodedHeaderText = null;
            bool changedHeaderText = false;
            bool encode = false;
            // if this is a header cell and we're htmlEncoding, htmlEncode the HeaderText before the base class tries to render it
            if (cellType == DataControlCellType.Header && SupportsHtmlEncode && HtmlEncode) {
                unencodedHeaderText = HeaderText;
                encode = true;
            }

            if (encode && !String.IsNullOrEmpty(unencodedHeaderText)) {
                _suppressHeaderTextFieldChange = true;
                HeaderText = HttpUtility.HtmlEncode(unencodedHeaderText);
                changedHeaderText = true;
            }
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            
            // restore the HeaderText property
            if (changedHeaderText) {
                HeaderText = unencodedHeaderText;
                _suppressHeaderTextFieldChange = false;
            }
            switch (cellType) {
                case DataControlCellType.DataCell:
                    InitializeDataCell(cell, rowState);
                    break;
            }
        }


        protected virtual void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) {
            Control childControl = null;
            Control boundControl = null;

            if (((rowState & DataControlRowState.Edit) != 0 && ReadOnly == false) || (rowState & DataControlRowState.Insert) != 0) {
                // 
                TextBox editor = new TextBox();
                editor.ToolTip = HeaderText;
                childControl = editor;

                if (DataField.Length != 0 && (rowState & DataControlRowState.Edit) != 0) {
                    boundControl = editor;
                }
            }
            else if (DataField.Length != 0) {
                boundControl = cell;
            }

            if (childControl != null) {
                cell.Controls.Add(childControl);
            }

            if (boundControl != null && Visible) {
                boundControl.DataBinding += new EventHandler(this.OnDataBindField);
            }
        }


        /// <devdoc>
        /// </devdoc>
        protected virtual void OnDataBindField(object sender, EventArgs e) {
            Control boundControl = (Control)sender;
            Control controlContainer = boundControl.NamingContainer;

            object data = GetValue(controlContainer);
            bool encodeValue = SupportsHtmlEncode && HtmlEncode && boundControl is TableCell;
            string dataValue = FormatDataValue(data, encodeValue);

            if (boundControl is TableCell) {
                if (dataValue.Length == 0) {
                    dataValue = "&nbsp;";
                }
                ((TableCell)boundControl).Text = dataValue;
            }
            else {
                if (!(boundControl is TextBox)) {
                    throw new HttpException(SR.GetString(SR.BoundField_WrongControlType, DataField));
                }
                
                if (ApplyFormatInEditMode) {
                    ((TextBox)boundControl).Text = dataValue;
                }
                else {
                    if (data != null) {
                        ((TextBox)boundControl).Text = data.ToString();
                    }
                }
                
                if (data != null) {
                    // size down the textbox for certain types
                    if (data.GetType().IsPrimitive) {
                        ((TextBox)boundControl).Columns = 5;
                    }
                }
            }
        }

        protected override void LoadViewState(object state) {
             // DevDiv Bugs 188902: Clear cached values as they may be stale after ViewState loads.
             _dataField = null;
             _dataFormatString = null;
             _htmlEncodeSet = false;
             _htmlEncodeFormatStringSet = false;

             base.LoadViewState(state);
        }

        /// <devdoc>
        /// <para>Override with an empty body if the field's controls all support callback.
        ///  Otherwise, override and throw a useful error message about why the field can't support callbacks.</para>
        /// </devdoc>
        public override void ValidateSupportsCallback() {
        }
    }
}